200 X 200 Matrix

9 views (last 30 days)
Ryan Wu
Ryan Wu on 22 Apr 2015
Commented: Ryan Wu on 30 Apr 2015
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;

Accepted Answer

Stephen23
Stephen23 on 22 Apr 2015
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
  1 Comment
Ryan Wu
Ryan Wu on 30 Apr 2015
Thanks so much for your help!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!