MATLAB Answers

0

how to sum a 9 matrice in a for loop ?

Asked by Nina Nina on 19 Nov 2019 at 14:51
Latest activity Commented on by KALYAN ACHARJYA on 19 Nov 2019 at 15:12
i have a 9 matrice (7 8 9;2 1 4; 5 6 3) and i have to write a code in a for loop that gives me the answer to the sum of all 9 numbers

  2 Comments

Do you means, you want the sum of all the values for a given matrix (for example, (7 8 9;2 1 4; 5 6 3)) ?
Nina Nina on 19 Nov 2019 at 15:04
yes, i need a code written in a for loop that when you give it a 9 matrice it gives you the sum of all numbers for example you give it the matrice above and it gives you 45 (the sum of those numbers)

Sign in to comment.

Tags

1 Answer

Answer by KALYAN ACHARJYA on 19 Nov 2019 at 14:59
Edited by KALYAN ACHARJYA on 19 Nov 2019 at 15:01

"May the question is: how to sum a 9 matrices elements in a for loop?"
No need of loop, here mat is given matrix
result=sum(mat(:))
Must be loop have to use, then,
Hints:
for i=1:r
for j=1:col
sum_mat=sum_mat+mat(i,j);
end
end

  2 Comments

Nina Nina on 19 Nov 2019 at 15:07
its a school assignment where i must use a for loop
Then Hints are sufficient, I will not tell you more. Please read Matlab documentation.

Sign in to comment.