create cell array of individual years from a long time series

3 ビュー (過去 30 日間)
Richard
Richard 2012 年 9 月 6 日
I have a matrix which is composed of a mixture of datevec and time series of measurements, e.g.
time = datevec(datenum('1966-01-01'):datenum('2009-12-03'));
data = rand(size(time,1),1);
Final = [time,data];
a = cell(length(unique(time(:,1))),1);
I would like to place all individual years in separate cells, I have created an empty cell array to house the values but am unsure about the most efficient way of moving the values from 'Final' to 'a'.
So far I have:
b = unique(Final(:,1));
for i = 1:length(a);
a{i} = Final(find(Final(:,1)==b(i)),:);
end
This works fine but I was wondering if anyone had an alternative method?

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 9 月 6 日
編集済み: Andrei Bobrov 2012 年 9 月 6 日
try this is code (corrected)
time = datevec(datenum('1966-01-01'):datenum('2009-12-03'));
data = rand(size(time,1),1);
Final = [time,data];
[c c c] = unique(Final(:,1));
a2 = accumarray(c,Final(:,end),[],@(x){x});
  2 件のコメント
Richard
Richard 2012 年 9 月 6 日
Great suggestion, although I am unsure about how accumarray works in this manner. From the example, if I do not have the variable 'data' only the variable 'Final' i.e. one matrix with the dates and data, do I need to separate them into 2 variables and then follow the answer you have shown or can 'a2' be altered to use the matrix 'Final' instead of the vector 'data'?
Andrei Bobrov
Andrei Bobrov 2012 年 9 月 6 日
corrected

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by