フィルターのクリア

Form a Cumulative sum matrix

1 回表示 (過去 30 日間)
Joseph Lee
Joseph Lee 2017 年 11 月 15 日
回答済み: ali fadaie 2017 年 11 月 24 日
S= 10x1300
R=10x1300
z1= S1+R1 x1300
z2=S1+R1+R1+S2+R2 x1300
z3=S1+R1+R1+S2+R2+R2+S3+R3 x1300
.
.
.
z10=S1+R1+R1+S2+R2+R2+S3+R3+.. +R9+S10+R10+R10 x 1300
Result: Form z, a 10x1300 matrix
S & R is a 10x1300 matrix of random numbers
First row of matrix z= First row of S + first row of R
Second row of matrix z= First row of S + first row of R + first row of R + second row of S + second row of R + second row of R
Each addition adds Previous row of R and the next row of S and R
  2 件のコメント
M
M 2017 年 11 月 15 日
And what is your question ?
Joseph Lee
Joseph Lee 2017 年 11 月 15 日
Hi, it is stated in title and in text, form a cumulative sum matrix z.

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

採用された回答

Stephen23
Stephen23 2017 年 11 月 15 日
編集済み: Stephen23 2017 年 11 月 15 日
>> S = randi(9,10,1300);
>> R = randi(9,10,1300);
>> Z = cumsum(S,1)+2*cumsum(R,1)-R;
>> Z(10,:) = Z(10,:)+R(10,:);
This correctly takes into account that each row Z(n,:) only adds row R(n,:) once, except for the tenth row which is a special case because R(10,:) is added twice.
Here is a complete example:
>> R = repmat((1:10).',1,3)
R =
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
>> S = repmat((1:3),10,1)
S =
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
>> Z = cumsum(S,1)+2*cumsum(R,1)-R;
>> Z(10,:) = Z(10,:)+R(10,:)
Z =
2 3 4
6 8 10
12 15 18
20 24 28
30 35 40
42 48 54
56 63 70
72 80 88
90 99 108
120 130 140
And checking against the given examples from rows one and three:
>> S(1,:)+R(1,:)
ans =
2 3 4
>> S(1,:)+R(1,:)+R(1,:)+S(2,:)+R(2,:)+R(2,:)+S(3,:)+R(3,:)
ans =
12 15 18

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2017 年 11 月 15 日
編集済み: Andrei Bobrov 2017 年 11 月 15 日
z = cumsum(S + 2*R) - [R(1:end-1,:); zeros(1,size(R,2))];
  2 件のコメント
Stephen23
Stephen23 2017 年 11 月 15 日
編集済み: Stephen23 2017 年 11 月 15 日
Almost, but does not take into account:
  • row N of R is not counted twice in row N of Z.
  • row 10 of R is counted twice in row 10 of Z.
Andrei Bobrov
Andrei Bobrov 2017 年 11 月 15 日
Thank you Stephen!
I'm corrected my code.

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


ali fadaie
ali fadaie 2017 年 11 月 24 日
Yes it is right

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by