I have got a matrix with values in.
The first column of the matrix is the date in the following form, 19260701 pr YYYYMMDD.
The other columns of the matrix are series.
19260702 0.026 0.000 NaN 1.175
19260706 0.009 0.000 NaN 1.842
19260707 1.388 0.001 NaN 9.061
19260708 1.147 0.028 NaN 0.067
19260709 0.604 0.018 NaN 0.000
19260710 7.255 0.020 NaN 0.005
19260712 0.085 0.093 NaN 1.832
19260713 0.163 0.025 NaN 3.897
19260714 1.294 0.545 NaN 0.188
19260715 0.256 0.077 NaN 0.001
19260716 0.001 0.002 NaN 0.018
19260717 0.000 0.015 NaN 1.863
19260719 0.002 0.062 NaN 1.465
19260720 2.761 0.028 NaN 6.453
19260721 1.998 0.067 NaN 0.328
19260722 0.160 0.123 NaN 0.651
19260723 0.009 0.000 NaN 0.001
19260724 0.005 0.000 NaN 0.000
19260726 0.016 0.002 NaN 0.860
19260727 0.022 0.000 NaN 0.329
19260728 0.002 0.001 NaN 0.857
19260729 0.000 0.343 NaN 2.125
19260730 0.002 0.001 NaN 1.265
19260731 0.000 0.000 NaN 0.283
19260802 0.000 0.010 NaN 0.815
19260803 0.000 1.020 NaN 27.701
19260804 0.000 0.197 NaN 4.162
19260805 0.027 0.016 NaN 42.120
19260806 0.046 0.200 NaN 15.163
19260807 0.284 0.004 NaN 0.382
19260809 1.330 0.000 NaN 3.102
19260810 1.066 0.016 NaN 0.035
19260811 0.261 0.119 NaN 0.249
19260812 0.014 0.031 NaN 328.139
19260813 0.024 0.042 NaN 40.248
19260814 0.094 0.047 NaN 1.460
19260816 0.042 0.007 NaN 25.928
Is it possible to Sum the values in each column of the matrix based on the month?
Apologise,
Following the comments,
I dont just want to sum by month, but each yearmonth, i.e sum Jan 1960, Feb 1960 etc.
You can use unique to get a label for each month, and then splitapply to accumulate values in the other columns based on month label (accumarray would work but only on one column).
If you consider April 2018 and April 2019 the same month:
month = mod(floor(data(:,1)/100-1), 100)+1;
[month_name, ~, month_id] = unique(month);
result = splitapply(@sum, data(:,2:end), month_id);
result_with_month = [month_name result];
If you consider April 2018 and April 2019 different months:
month = floor(data(:,1)/100);
[month_name, ~, month_id] = unique(month);
result = splitapply(@sum, data(:,2:end), month_id);
result_with_month = [month_name result];
Example results with the provided data:
result =
1.0e+02 *
0.172050000000000 0.014510000000000 NaN 0.345660000000000
0.031880000000000 0.017090000000000 NaN 4.895040000000000
result_with_month =
1.0e+05 *
1.926070000000000 0.000172050000000 0.000014510000000 NaN 0.000345660000000
1.926080000000000 0.000031880000000 0.000017090000000 NaN 0.004895040000000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With