How to add columns of accumulated sum in a matrix

1 回表示 (過去 30 日間)
Philippe Corner
Philippe Corner 2018 年 1 月 23 日
コメント済み: Star Strider 2018 年 1 月 24 日
If a have this matrix:
A=[1:1:100]
B=[101:1:200]
M=[A(:), B(:)]
I need to add 20 columns to M which contain the summation of A values like:
C = summation of the row |i| + the row |i-1|,
D = summation of the row |i| + the row |i-1|+|i-2|,
E = summation of of the row |i| + the row |i-1|+|i-2|+|i-3|,.. etc
and M=[A(:), B(:), C(:), D(:), E(:) ...etc]
For example: it would be something like:
A B C D E.. etc -> until 20
1 101 1 1 1
2 102 3 3 3
3 103 5 6 6
4 104 7 9 10
5 105 9 12 14
6 106 11 15 18
7 107 13 18 22
8 108 15 21 26
9 109 17 24 30
10 110 19 27 34
11 111 21 30 38
12 112 13 33 42
. . . . .
. . . . .
. . . . .
etc etc etc etc etc ... etc -> until 20
  2 件のコメント
Star Strider
Star Strider 2018 年 1 月 23 日
Note: this is not homework. It’s meteorology or climatology research.
Philippe Corner
Philippe Corner 2018 年 1 月 23 日
Yep, im trying to analyze some climate data. But for my bachelor thesis. I already did a cumsum, but there are too many columns, so im looking for a more automatic way to do it..
Thanks in advance Mr Star.

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

採用された回答

Star Strider
Star Strider 2018 年 1 月 24 日
This seems to work for ‘C’, ‘D’ ... ‘T’.
This construction seems the only way to form your matrix (that I call ‘Ar’ here).
A = (1:100)';
Ar(1,:) = ones(1, 20);
for k1 = 2:numel(A)
if k1 <= 20
v1 = cumsum(A(k1:-1:max(1,k1-19)));
Ar(k1,:) = [v1(2:end)' v1(end)*ones(1, 21-k1)];
else
v1 = cumsum(A(k1:-1:1));
Ar(k1,:) = v1(2:21)';
end
end
  2 件のコメント
Philippe Corner
Philippe Corner 2018 年 1 月 24 日
crack.
Star Strider
Star Strider 2018 年 1 月 24 日
Thank you!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 1 月 23 日
Sounds like homework. See if you can figure out how to use the cumsum() function to do it. Let us know if you really can't figure it out.
  1 件のコメント
Philippe Corner
Philippe Corner 2018 年 1 月 23 日
I already did with cumsum(), but im trying to figure it out with more columns and im trying to automatize the code for that.

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

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by