How to add rows of data in a 5x5 Matrix
1 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153961/image.png)
0 个评论
采纳的回答
James Tursa
2016-2-3
If you want to replace the inf values with 0 first, then
A(isinf(A)) = 0;
3 个评论
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])));
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!