How to add row values in matrix using a loop?

I have matrix A
A = [ 1 1 2 0.1;
2 2 3 0.02;
3 3 4 0.05;
4 4 5 0.3;
5 2 6 0.5;
6 2 7 0.4;
7 7 8 0.2;
8 3 9 0.9;
9 4 10 1.3
];
I want to add row values like that using a loop..
X1 = A(1,4)
X2 = A(1,4)+A(2,4)
X3 = A(1,4)+A(2,4)+A(3,4)
X4 = A(1,4)+A(2,4)+A(3,4)+A(4,4)
X5 = A(1,4)+A(2,4)+A(5,4)
X6 = A(1,4)+A(2,4)+A(6,4)
X7 = A(1,4)+A(2,4)+A(6,4)+A(7,4)
X8 = A(1,4)+A(2,4)+A(3,4)+A(8,4)
X9 = A(1,4)+A(2,4)+A(3,4)+A(4,4)+A(4,9)
B= [ X1;
X2;
X3;
X4;
X5;
X6;
X7;
X8;
X9;
];
How can I do that? Any tips?
Many Thanks in advance

5 件のコメント

Image Analyst
Image Analyst 2017 年 1 月 3 日
There doesn't seem to be any rule for which elements of A you pick in getting the B's. It seems to follow a rule like you could say B = cumsum(A(:,4)) through X4 but then everything breaks down after X5 with indexes seeming to be chosen at random. What is the rule? Or are there just tons of typos in your indexes?
Yuli Hartini
Yuli Hartini 2017 年 1 月 3 日
編集済み: Yuli Hartini 2017 年 1 月 3 日
I want a number will follow the lines of A (:, 1), and will cease in A (:, 2).
How can I do that?
Walter Roberson
Walter Roberson 2017 年 1 月 3 日
According to that rule,
X7 = A(1,4)+A(2,4)+A(6,4)+A(7,4)
should be
X7 = A(1,4)+A(2,4)+A(3,4)+A(4,4)+A(5,4)+A(6,4)+A(7,4)
since you have 7 7 8 0.2 for that row.
I can see how you might be defining the other rows, but not that one.
Yuli Hartini
Yuli Hartini 2017 年 1 月 3 日
How about this: Number will follow the lines of A (:, 1), and will cease in A (:, 2). When A(:,2)=(A(:,3)+1)
How can I do that?
Walter Roberson
Walter Roberson 2017 年 1 月 3 日
None of your examples have A(:,2)=(A(:,3)+1) . In some cases A(:,2)=(A(:,3)-1) .
Your proposed rule is not clear on what to do when the relationship between those two columns does not hold.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2017 年 1 月 2 日

コメント済み:

2017 年 1 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by