Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Determining answers from a for loop?

4 ビュー (過去 30 日間)
Marlee Shaffer
Marlee Shaffer 2017 年 3 月 15 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I was given a practice midterm exam with sample questions, and the last one gave me a code and asked me to determine what the variable maximum would contain. The code was:
if true
% code
end
clear
A = [10 1 7;
4 3 6;
6 9 2];
[rows,cols] = size(A);
for c = 1:cols
maximum(c) = A(1,c);
for r = 1 : rows
if A(r,c) < maximum(c)
maximum(c) = A(r,c);
end
end
end
disp(maximum)
I plugged it into Matlab to get the variable maximum to be [4, 1, 2], however I do not get Matlab to use on my midterm so I have to know why it gives me those numbers. Could someone please explain why I got the numbers I did when I ran the code, and how I should go about tackling a loop question like this?? Thanks!

回答 (1 件)

Roger Stafford
Roger Stafford 2017 年 3 月 15 日
Whoever devised that problem has a fiendish sense of humor. It is actually calculating the minimum of each of the three columns in A. For example, for c = 1, indicating the first column of A, it starts “maximum(c)” with the first row element of that column. If the next column element is smaller, that smaller element replaces the “maximum(c)” value. When it finishes, it obviously has the minimum, not maximum, value for the column, which in this case is 4. This is something you can do in your head without using matlab.

この質問は閉じられています。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by