finding the position of the minimum element in the matrix

48 ビュー (過去 30 日間)
Umut Oskay
Umut Oskay 2020 年 4 月 12 日
コメント済み: madhan ravi 2020 年 4 月 12 日
hello. i need to know the position of the minimum element of the matrix. For example
M= [1 5 7; 2 4 5 ]
M=
1 5 7
2 4 5
the min is 1 and it's position is (1,1).
How can i find this?
Thanks

採用された回答

Image Analyst
Image Analyst 2020 年 4 月 12 日
You really should use find() rather than min() since find() will find them all and min() only finds the first occurrence, which is important since the min could occur at multiple locations:
M = [1 5 7; 2 4 5; 4, 1, 5]
minValue = min(M(:));
% Find all (row, column) pairs where M = the min value.
[rows, columns] = find(M == minValue)
% Print them out:
for k = 1 : length(rows)
fprintf('M equals the min value of %f at row = %d, column = %d.\n', ...
minValue, rows(k), columns(k));
end
You'll see
M =
1 5 7
2 4 5
4 1 5
rows =
1
3
columns =
1
2
M equals the min value of 1.000000 at row = 1, column = 1.
M equals the min value of 1.000000 at row = 3, column = 2.

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 4 月 12 日
編集済み: madhan ravi 2020 年 4 月 12 日
MIN = min( M(:) );
[ Row, Column ] = ind2sub( size(M), find(MIN == M, 1 ) )
  2 件のコメント
Umut Oskay
Umut Oskay 2020 年 4 月 12 日
Can you explain your answer a bit ?
madhan ravi
madhan ravi 2020 年 4 月 12 日
doc find
doc min
doc ind2sub

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by