Finding maximum value and it's location from the matrix

586 ビュー (過去 30 日間)
Kantosa
Kantosa 2013 年 12 月 7 日
移動済み: Dyuman Joshi 2023 年 11 月 13 日
Hi,
This is the matrix I obtained
K =
8 16 -16 -8 -16 16 -20 -40 40
18 2 -6 -18 -2 6 -45 -5 15
12 20 20 -12 -20 -20 -30 -50 -50
-4 -8 8 4 8 -8 0 0 0
-9 -1 3 9 1 -3 0 0 0
-6 -10 -10 6 10 10 0 0 0
-16 -32 32 -8 -16 16 0 0 0
-36 -4 12 -18 -2 6 0 0 0
-24 -40 -40 -12 -20 -20 0 0 0
The question asked me to find the maximum number and it's location using the max function. I did this by using this code:
max_num=max(K(:))
[X Y]=ind2sub(size(K),max_num)
From the code, I got the maximum value off from the matrix, however the location is not right.
max_num =
40
X =
4
Y =
5
The X and Y should have display X = 9 , Y = 1 , instead it displays X = 4 , Y = 5. I don't know what is wrong with my code. It would be great if anyone can help me with this.
Thank you in advance.

採用された回答

sixwwwwww
sixwwwwww 2013 年 12 月 7 日
編集済み: Image Analyst 2021 年 10 月 19 日
Try this:
K = [...
8 16 -16 -8 -16 16 -20 -40 40
18 2 -6 -18 -2 6 -45 -5 15
12 20 20 -12 -20 -20 -30 -50 -50
-4 -8 8 4 8 -8 0 0 0
-9 -1 3 9 1 -3 0 0 0
-6 -10 -10 6 10 10 0 40 0
-16 -32 32 -8 -16 16 0 40 0
-36 -4 12 -18 -2 6 0 0 0
-24 -40 -40 -12 -20 -20 0 0 0]
K = 9×9
8 16 -16 -8 -16 16 -20 -40 40 18 2 -6 -18 -2 6 -45 -5 15 12 20 20 -12 -20 -20 -30 -50 -50 -4 -8 8 4 8 -8 0 0 0 -9 -1 3 9 1 -3 0 0 0 -6 -10 -10 6 10 10 0 40 0 -16 -32 32 -8 -16 16 0 40 0 -36 -4 12 -18 -2 6 0 0 0 -24 -40 -40 -12 -20 -20 0 0 0
[row, col] = find(ismember(K, max(K(:))))
row = 3×1
6 7 1
col = 3×1
8 8 9
  3 件のコメント
li weilin
li weilin 2020 年 3 月 2 日
Thank you. This is the right answer!
NAGENDRA KUMAR
NAGENDRA KUMAR 2021 年 6 月 18 日
Thank You so much , It works

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

その他の回答 (3 件)

the cyclist
the cyclist 2013 年 12 月 7 日
You are very close.
Big hints:
In your first line of code,
>> max_num=max(K(:));
you are finding the value , but not the index , of the maximum. If you call max() with two output arguments, then you will also get the index.
>> [max_num,max_idx] = max(K(:));
In your second line of code,
>> [X Y]=ind2sub(size(K),max_num);
you are using a function that converts a linear index to (x,y) coordinates. But you have not put the index into that function; you have put the value into that function.
I think that should get you there.

Kan-Hua
Kan-Hua 2013 年 12 月 7 日
編集済み: Kan-Hua 2013 年 12 月 7 日
I think that's what you need:
[max_num, max_idx]=max(K(:));
[X,Y]=ind2sub(size(K),max_idx);
you need the input parameter of ind2sub to be an index rather than maximum value.
Alternatively, you can do this:
[max_num, max_idx]=max(K(:));
[X,Y]=ind2sub(size(K),find(K==max_num));
  1 件のコメント
GS76
GS76 2019 年 9 月 26 日
移動済み: Dyuman Joshi 2023 年 11 月 13 日
Thank you Kan-Hua,
This was exactly what I needed to solve my problem, much appreciated.

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


Stylianos Assimonis
Stylianos Assimonis 2021 年 10 月 19 日
編集済み: Stylianos Assimonis 2021 年 10 月 19 日
[t1,u1]=max(K);
[t2,u2]=max(t1);
Maximum lies at [u1(u2),u2], i.e.,
K_max = K(u1(u2),u2).

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by