matching vectors of different length
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!