matching vectors of different length

3 ビュー (過去 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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by