How to add rows of data in a 5x5 Matrix

1 回表示 (過去 30 日間)
Arsal15
Arsal15 2016 年 2 月 3 日
コメント済み: Arsal15 2016 年 2 月 3 日
Hi, I want to add entries in each row and put zero in the Inf place but I want to use for loop? I tried this
if true
A = sum(A,2);
end
But I am want to put entries equal to Inf to zero and then add them.

採用された回答

James Tursa
James Tursa 2016 年 2 月 3 日
If you want to replace the inf values with 0 first, then
A(isinf(A)) = 0;
  3 件のコメント
Guillaume
Guillaume 2016 年 2 月 3 日
編集済み: Guillaume 2016 年 2 月 3 日
Wow! You like copy pasting . As soon as you start writing lines of code that only differ according to a pattern, you have to think that the program can do that for you.
The whole of the above code could be replaced by these two lines:
rows = [1, 2, 3, 1, 1]; %row to sum
sumA(rid, tid) = sum(A(rows(rid), setdiff(1:5, [rid tid])));
And if you made a typo and forgot to replace the row number in the case 4 and case 5 (another reason for not copy-pasting redundant code), then it's even simpler:
sumA(rid, tid) = sum(A(rid, setdiff(1:5, [rid tid])));
Arsal15
Arsal15 2016 年 2 月 3 日
Thanks alot Guillaume :)
I think in the rows = [1,2,3,4,5] ?? You solved my issue and what if I have to choose a non zero entry of the matrix from a 5x5 matrix. Only one non zero and all zeros and I have to pick that and use for my calculations.

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

その他の回答 (0 件)

カテゴリ

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

Translated by