Write a for loop that is equivalent to the command sum(A),whereA is a matrix.

Im having trouble understanding this question T4.5-4from the text i just need some help and steps on how to find the answers.

Answers (1)

When you need to make a sum using a for cycle, it is necessary to initialize an accumulator variable to zero. This will allow you to perform your summation. The example I put below shows it. , it is added in columns.
A=magic(5);
SUM=zeros(1,5);
for k=1:5
for r=1:5
SUM(1,k)=SUM(1,k)+A(r,k);
end
end
SUM

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 14 Nov 2017

Community Treasure Hunt

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

Start Hunting!