Find the maximum value and its location from a matrix using two nested loops

Hi,
Right now I have this matrix K
K =
-3 -8 6 -3 -8 6 9 24 -18
-5 10 -9 -5 10 -9 15 -30 27
8 4 9 8 4 9 -24 -12 -27
-9 -24 18 12 32 -24 3 8 -6
-15 30 -27 20 -40 36 5 -10 9
24 12 27 -32 -16 -36 -8 -4 -9
9 24 -18 -15 -40 30 0 0 0
15 -30 27 -25 50 -45 0 0 0
-24 -12 -27 40 20 45 0 0 0
I wonder how I can use two nested loops to find the maximum number and its location from this matrix.
It would be great if anyone can help me with this
Thank you very much :)

 採用された回答

sixwwwwww
sixwwwwww 2013 年 12 月 8 日
編集済み: sixwwwwww 2013 年 12 月 8 日
you can do it as follow:
MaxValue = -Inf;
row = 0;
column = 0;
for i = 1:size(K, 1)
for j = 1:size(K, 2)
if K(i, j) > MaxValue
MaxValue = K(i, j);
row = i;
column = j;
end
end
end

6 件のコメント

Jan
Jan 2013 年 12 月 8 日
Please do not solve homework questions. Now Kantosa cannot submit this solution without cheating anymore and the effect for his learning experience is not helpful anymore.
sixwwwwww
sixwwwwww 2013 年 12 月 8 日
Oh sorry but there was no homework tag with it. How can I know is it homework or user just asking question for learning? I thought that he is not sure that finding maximum is possible with two nested loops or not so I just showed that it is possible
Image Analyst
Image Analyst 2013 年 12 月 8 日
編集済み: Image Analyst 2013 年 12 月 8 日
This is the third time this person has asked this question. I suspected it was homework that's why I gave hints like in http://www.mathworks.com/matlabcentral/answers/109031#answer_117670, rather than an outright solution. The person accepted it so I don't know why it was asked again , and asked again without the homework tag even when I explicitly mentioned it. Kantosa, any response to that?
Kantosa
Kantosa 2013 年 12 月 8 日
Yes it is a homework. I am looking for a way to solve it but not a solution. The reason I posted the question again is to find different ways to solve it (different hints). I am new here and not sure about how this work so I keep posting the question again. Sorry for any misunderstanding. Next time I will put the homework tag down.
Image Analyst
Image Analyst 2013 年 12 月 8 日
But you didn't ask for other ways - you rigidly specified the way. The other, more MATLAB-ish way would be to use the max() function. Look at the two arguments it returns. You might also find ind2sub() helpful,
Alexandra Vaupotic
Alexandra Vaupotic 2021 年 2 月 8 日
編集済み: Alexandra Vaupotic 2021 年 2 月 8 日
How could you make this a function with an output of the Max value?

サインインしてコメントする。

その他の回答 (1 件)

Khalid Mojallid
Khalid Mojallid 2019 年 3 月 26 日

0 投票

MaxValue = -Inf;
row = 0;
column = 0;
for i = 1:size(K, 1)
for j = 1:size(K, 2)
if K(i, j) > MaxValue
MaxValue = K(i, j);
row = i;
column = j;
end
end
end

1 件のコメント

Nathan Blais
Nathan Blais 2019 年 10 月 3 日
I'm guessing K would be the vector we are trying to find the max value for?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

タグが未入力です。

質問済み:

2013 年 12 月 8 日

編集済み:

2021 年 2 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by