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

2 views (last 30 days)
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)

Rafael Hernandez-Walls
Rafael Hernandez-Walls on 14 Nov 2017
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 MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!