Daily values to monthly sums
2 ビュー (過去 30 日間)
古いコメントを表示
Very silly issue I'm having, clearly it's Friday and my brain is dead!
I have yearly data (column 1 below is year, col 2 is DoY, col 3 is what I want, col 4 is a running ref #)
1999 88 -0.153775166948929 88
1999 89 0.0645182041737496 89
1999 90 0.305720459521316 90
1999 91 0.608267678784863 91
1999 92 -0.105377378067329 92
1999 93 0.164670217562409 93
1999 94 0.486056906589846 94
1999 95 -0.153775166948929 95
1999 96 0.0645182041737496 96
1999 97 0.305720459521316 97
1999 98 0.608267678784863 98
1999 99 -0.0388209349360338 99
1999 100 -0.0388209349360338 100
1999 101 -0.220461941261943 101
1999 102 -0.0576523900004068 102
1999 103 0.896360903826591 103
1999 104 0.842649481758915 104
1999 105 -0.347869165344918 105
What I want to do is pull out monthly totals of column 3. I wrote some indices:
%Monthly indexes
iJan=(1:1:31);
iFeb=(32:1:59);
iFebl=horzcat(iFeb,29);
iMar=(60:1:90);
iApr=(91:1:120);
iMay=(121:1:151);
iJun=(152:1:181);
iJul=(182:1:212);
iAug=(213:1:243);
iSep=(244:1:273);
iOct=(274:1:304);
iNov=(305:1:334);
iDec=(335:1:365);
I need monthly values for every month in my dataset. I keep on writing loops that do very silly things, please save my brain from friday frustration!
2 件のコメント
採用された回答
Cedric
2013 年 10 月 25 日
編集済み: Cedric
2013 年 10 月 25 日
Well, TGIF anyway! Assuming that iFeb1 is irrelevant (if not, the solution can be updated).
% - Vector of months start days.
monthsStart = [1,32,60,91,121,152,182,213,244,274,305,335] ;
% - Build lookup table day/month: e.g. LT(47) is month ID for day 47.
LT = zeros( 365, 1 ) ;
LT(monthsStart) = 1 ;
LT = cumsum( LT ) ;
% - Build vector of month ID for data entries.
monthId = LT(data(:,2)) ;
% - Accumulate using month ID as index.
sums = accumarray( monthId, data(:,3), [12,1] ) ;
2 件のコメント
Cedric
2013 年 10 月 28 日
編集済み: Cedric
2013 年 10 月 28 日
If you understood my answer with ACCUMARRAY (let me know if it's not fully the case) the update for managing years will be really simple, because the first argument of indices can have as many columns as there are dimensions in the accumulator.
This means that you can do something like..
yearId = data(:,1) - min( data(:,1) ) + 1 ;
nYear = range( data(:,1) ) + 1 ;
for creating year IDs starting at 1, and then
sums = accumarray( [monthId,yearId], data(:,3), [12,nYear] ) ;
for building a 2D array with one row per month and one column per year.
Note that if you had huge gaps between years, e.g. 1920, 1960, 2000, you could build a look up table for mapping the 81 years over year IDs 1,2,3 instead of 1:81.
Let me know if you need more information.
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!