i want to find the max element in each row and position of that element in terms of row and column

1 回表示 (過去 30 日間)
i am giving the example what i have tried and what result i have got
c= 2 3 4 5
67 58 79 81
91 23 56 78
[max_num,max_idx]=max(c,[],2)
max_num =
5
81
91
max_idx =
4
4
1
>> [X,Y]=ind2sub(size(c),max_idx)
X =
1
1
1
Y =
2
2
1
i am not getting the right ans in first row max element is 5 postion will be (1,4)

採用された回答

Image Analyst
Image Analyst 2017 年 8 月 22 日
編集済み: Image Analyst 2017 年 8 月 22 日
You made a common novice user mistake: mistaking (row, column) for (x, y). Note: row is Y, not X. and column is X, not Y. ind2sub() returns [row, column], NOT [X, Y]. So you might be tempted to do:
[Y, X] = ind2sub(size(c), max_idx)
however even that is not right. Why not? Because max_idx is not the linear index over the whole matrix - it is the column number of each row. For example while max() might give [4,4,1], the linear indexes would be [4,8,9]. But that is not what you were passing in to ind2sub, you were passing in [4,4,1], so it didn't give you the expected output.
To correct the code, see the solution Star Strider gave you.
  2 件のコメント
praveen rai
praveen rai 2017 年 8 月 22 日
good xplanation didnt get this line'see the solution Star Strider gave you'.
praveen rai
praveen rai 2017 年 8 月 22 日
sorry i got what it means 'see the solution Star Strider gave you'.thnx

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

その他の回答 (2 件)

Star Strider
Star Strider 2017 年 8 月 22 日
The max function is returning the column numbers of the maximum in each row, because you asked it to. If you want to return the row and column of the row maxima, you have to provide the row indices:
rc_max = [(1:size(c,1))' max_idx]
rc_max =
1 4
2 4
3 1
That should be the result you want.

Walter Roberson
Walter Roberson 2017 年 8 月 22 日
X = 1:size(c, 1)
Y = max_idx

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by