根据条件语句Matlab对列的部分求和
Sum parts of column depending on conditional statement Matlab
我在 matlab 中有一个矩阵,如果其他列使用条件语句检查,我想对列的值求和,然后我想以连贯的方式存储求和值。
布局输入table ListMax:
year Month day hour precipitation
1998 1 1 1 5
1998 1 2 2 7
1998 1 3 3 0
.... ... ... ... ...
布局输出result_matrix:
year jan feb mar
1998 100 120 140
1999 90 110 130
... ... ... ...
我正在努力处理遍历行、检查条件语句和将值保存在输出中的组合。
我有以下想法,但没有用:
result_matrix = zeros(15,12); %empty matrix 15 years, 12 months
year_number = 1998;
counting_years = 1;
counting_months = 1;
for (ii = 1: length(ListMax));
if ListMax(:,1) == year_number && ListMax(:,2) == counting_months;
result_matrix(counting_years, counting_months) = (sum(Listmax(:,5));
else % update month itirators
if counting_months < 12
counting_months = counting_months + 1;
else % end of year, set month count to 1
counting_months = 1;
end
year_number = year_number +1;
counting_years = counting_years + 1;
end
end
我知道这可能不是最直接的方法,但目前这是我能想到的方法,现在我只需要让它工作。任何朝着正确方向的推动将不胜感激:)
这个怎么样(未测试):
year_start = 1998;
year_end = 2016;
result_matrix = zeros(year_end-year_start+1,12);
for year = year_start:year_end
for month = 1:12
rows = (ListMax(:,1)==year) & (ListMax(:,2)==month);
result_matrix(year-year_start+1,month) = sum(ListMax(find(rows),5));
end
end
您可以简单地使用带有两列子索引的 accumarray
。
year Month day hour precipitation
1998 1 1 1 5
1998 1 2 2 7
1998 1 3 3 0
.... ... ... ... ...
例如,如果你想根据年份和月份求和总降水量,你可以使用矩阵的第一列作为索引,最后一列作为降水量的值。
sub = 1998 1 %Jan.1998
1998 1 %Jan.1998
1998 2 %Feb.1998
...
sub(:,1) = sub(:,1) - (min(sub(:,1))-1); %so the index for the year start at 1 and not 1998.
val = 6 %millimeter of precipitation on Jan.1998 day xxx
4 %millimeter of precipitation on Jan.1998 day xxx
7 %millimeter of precipitation on Feb.1998 day xxx
....
现在使用 accumarray
:
result = accumarray(sub,val,[],@sum);
我在 matlab 中有一个矩阵,如果其他列使用条件语句检查,我想对列的值求和,然后我想以连贯的方式存储求和值。
布局输入table ListMax:
year Month day hour precipitation
1998 1 1 1 5
1998 1 2 2 7
1998 1 3 3 0
.... ... ... ... ...
布局输出result_matrix:
year jan feb mar
1998 100 120 140
1999 90 110 130
... ... ... ...
我正在努力处理遍历行、检查条件语句和将值保存在输出中的组合。
我有以下想法,但没有用:
result_matrix = zeros(15,12); %empty matrix 15 years, 12 months
year_number = 1998;
counting_years = 1;
counting_months = 1;
for (ii = 1: length(ListMax));
if ListMax(:,1) == year_number && ListMax(:,2) == counting_months;
result_matrix(counting_years, counting_months) = (sum(Listmax(:,5));
else % update month itirators
if counting_months < 12
counting_months = counting_months + 1;
else % end of year, set month count to 1
counting_months = 1;
end
year_number = year_number +1;
counting_years = counting_years + 1;
end
end
我知道这可能不是最直接的方法,但目前这是我能想到的方法,现在我只需要让它工作。任何朝着正确方向的推动将不胜感激:)
这个怎么样(未测试):
year_start = 1998;
year_end = 2016;
result_matrix = zeros(year_end-year_start+1,12);
for year = year_start:year_end
for month = 1:12
rows = (ListMax(:,1)==year) & (ListMax(:,2)==month);
result_matrix(year-year_start+1,month) = sum(ListMax(find(rows),5));
end
end
您可以简单地使用带有两列子索引的 accumarray
。
year Month day hour precipitation
1998 1 1 1 5
1998 1 2 2 7
1998 1 3 3 0
.... ... ... ... ...
例如,如果你想根据年份和月份求和总降水量,你可以使用矩阵的第一列作为索引,最后一列作为降水量的值。
sub = 1998 1 %Jan.1998
1998 1 %Jan.1998
1998 2 %Feb.1998
...
sub(:,1) = sub(:,1) - (min(sub(:,1))-1); %so the index for the year start at 1 and not 1998.
val = 6 %millimeter of precipitation on Jan.1998 day xxx
4 %millimeter of precipitation on Jan.1998 day xxx
7 %millimeter of precipitation on Feb.1998 day xxx
....
现在使用 accumarray
:
result = accumarray(sub,val,[],@sum);