MATLAB: 200 X 200 Matrix

homeworkMATLABmatricessum

Hello All,
I would really appreciate any help on this question.
1. Generate a 200 x 200 matrix a random integers between -100 and 100.
2. Use the built-in command sum to determine the total sum of all the numbers in this matrix.
3. Put the command “tic” and “toc” right before the sum and after
4. Use nested loops to calculate the sum by adding one number at a time.
6. Display the two results to make sure they are equal.
This is what I have so far. I don't understand how to do the loop to calculate the sum.
tic;
x = randi([-100, 100], 200, 200)
toc;

Best Answer

Note that your code does not follow the guidelines given to you: the tic and toc are supposed to be before and after the sum statement, but you have put them around the randi statement.
You might end up with something like this:
R = 200;
C = 200;
X = randi([-100,100], R, C);
% sum in one statement
tic
Y = sum(X(:));
toc
% sum in a loop
Z = 0;
tic
for rr = 1:R
for cc = 1:C
Z = Z + X(rr,cc);
end
end
toc
Related Question