matching vectors of different length

3 views (last 30 days)
Tiina
Tiina on 12 Dec 2011
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

Answers (1)

Andrei Bobrov
Andrei Bobrov on 12 Dec 2011
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 Comments
Dr. Seis
Dr. Seis on 13 Dec 2011
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 on 14 Dec 2011
Yea thank you it works with [B B B] I was doing [B].

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!