Given the matrix:
A=[1 2 3; 4 5 6; 7 8 9]
- how would you use the for loop to compute the sum of the elements in matrix?
- write a one line MATLAB command
using the function sum() to sum the
matrix elements in
A
My answer:
1)
for j=1:3,
for i=j:3,
A(i,:) = A(i,:)+A(j+1,:)+A(j+2,:)
end
end
2)
sum(A)
Is that the correct answer?
I didn't know how to use if
, while
and for
...
Can anyone explain it to me?