200 X 200 Matrix
9 views (last 30 days)
Show older comments
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;
0 Comments
Accepted Answer
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
More Answers (0)
See Also
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!