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
Run Code Online (Sandbox Code Playgroud)
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.
您可以使用unique获取每个月的标签,然后splitapply基于月份标签在其他列中累积值(accumarray可以,但仅在一个列上有效)。
如果考虑同一个月的 2018年4月和2019年4 月:
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];
Run Code Online (Sandbox Code Playgroud)
如果考虑2018年4月和2019年4月的不同月份:
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];
Run Code Online (Sandbox Code Playgroud)
提供的数据的示例结果:
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
Run Code Online (Sandbox Code Playgroud)
这是一个典型的示例,您可以在其中使用表而不是矩阵。
A = [19260702 0.026 0.000 NaN 1.175
...
19260816 0.042 0.007 NaN 25.928];
% Matrix to table conversion
T = array2table(A,'VariableNames',{'DATE','S1','S2','S3','S4'});
% We add a new column "month"
T.MONTH = floor(T.DATE/100);
% varfun can apply a custom function to your table and group the result according
% to one (or more) variable(s)
Result = varfun(@sum,T,'InputVariables',{'S1','S2','S3','S4'},'GroupingVariables','MONTH')
Run Code Online (Sandbox Code Playgroud)
懒家伙选项:
如果您很懒惰并且不想手动添加变量的名称,则还可以使用线性索引:
T = array2table(A) %column name will be 'A1','A2',....
T.MONTH = floor(T{:,1}/100);
Result = varfun(@sum,T,'InputVariables',2:5,'GroupingVariables','MONTH')
Run Code Online (Sandbox Code Playgroud)
高尔夫代码选项
如果您喜欢短代码,则可以缩短参数名称。例如,if InputVariables是唯一以开头的参数名称,I您可以简单地使用I代替InputVariables。
Result = varfun(@sum,T,'I',2:5,'G',6)
Run Code Online (Sandbox Code Playgroud)