matching vectors of different length

조회 수: 1 (최근 30일)
Tiina
Tiina 2011년 12월 12일
Hi
I kindly need some guidance for a problem as the one below. I am using a very large data set and perhaps the following description is the closest to the problem at hand
dates: A=[1 1 1 2 2 2 3 3 4 4 4 5 5 6 6 6]
Unique dates: B=[1 2 3 4 5 6]
Units corresponding to unique dates: C=[12 18 15 14 11 17]
Output : unit values in C corresponding to unique dates in B such as they match the order of dates appearing in A.
output= [12 12 12 18 18 18 15 15 14 14 14 11 11 17 17 17]
Thank you for any input

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 12월 12일
A = [1 1 1 2 2 2 3 3 4 4 4 5 5 6 6 6];
C=[12 18 15 14 11 17];
output= C(A);
e.g. [ EDIT 08:16MDT 13.12.2011 ]
A = randi([18 23],15,1)
[B B B] = unique(A)
C = randi(125,max(B),1)
output = C(B)
  댓글 수: 3
Dr. Seis
Dr. Seis 2011년 12월 13일
If your "A" matrix does not include numbers ranging from 1 : length(C), then you will most certainly get that error. Using the example above, if A = [11 11 11 12 12 12 13 13 14 14 14 15 15 16 16 16], then you could not simply do C(A) to obtain the desired result because there is no 11th, 12th, ..., 16th element - there are only 6 elements in C. You would have to us Andrei's line involving "UNIQUE" to obtain a vector with the appropriate numbers for indexing.
Tiina
Tiina 2011년 12월 14일
Yea thank you it works with [B B B] I was doing [B].

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by