matrix element summation algorithm

Hi!
I have a matrix A [ 1 2 3; 3 1 2; 1 2 1]. I want to get the sum of A(1,1)+A(2,1) + A(3,3), which will be the result of a general algorithm which will follow the following logic: since A(1,1)=1, the second term in the sum will be the second row of A, and column 1 since A(1,1)=1. Therefore the second term in the sum will be A(2,1). Then, since A(2,1)=3, the third term in the sum will be the third row of A, and it's 3rd column, since A(2,1)=3 (in other words, the A(.,.) value determines the column from which the term will be taken, and we move successively down the matrices rows).
I hope I managed to explain this!
Thanks,
Filippos

1 件のコメント

Image Analyst
Image Analyst 2018 年 7 月 22 日
Sounds like homework. If it is, please read this link. If it's not, what is the question? There is no question there, just an announcement of what you're going to do, presumably with a simple for loop.

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

回答 (1 件)

madhan ravi
madhan ravi 2018 年 7 月 22 日
編集済み: madhan ravi 2018 年 7 月 22 日

0 投票

Hi try this :
sum(diag(A))

1 件のコメント

madhan ravi
madhan ravi 2018 年 10 月 11 日
if it worked accept the answer

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

カテゴリ

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

質問済み:

2018 年 7 月 22 日

コメント済み:

2018 年 10 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by