How to find the index of the first absolute minimum entry of a matrix

20 ビュー (過去 30 日間)
Tarek Hajj Shehadi
Tarek Hajj Shehadi 2022 年 5 月 19 日
コメント済み: Stephen23 2022 年 5 月 19 日
I am working on a model structure optimization problem and to summarize consider this matrix:
A=[5,7,0.5,5 ; 2,0.5,4,1 ; 0.5,6,7,9]
In this matrix, 0.5 is clearly the smallest element of this matrix.
It is repeated 3 times.
Question: Can someone provide me with an algorithm that returns the index and value of this absolute minimum "only the first time it appears"
For instance:
A=[5,7,0.5,5 ; 2,0.5,4,1 ; 0.5,6,7,9];
[value,index]=somecode(A)
%returns the value of the absolute minimum
>> value = 0.5
%returns the index of the first time this absolute minimum appears
>> (3,1) % the first time it appears is in column 3 row 1 and NOT column 2 row 2
  1 件のコメント
Stephen23
Stephen23 2022 年 5 月 19 日
Your example matrix is not very good because it contains the value 0.5 in both (1,3) and (3,1) locations.
Catalytic's answer for example gives the wrong answer because of this confusion.

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

採用された回答

Catalytic
Catalytic 2022 年 5 月 19 日
編集済み: Catalytic 2022 年 5 月 19 日
A=[5,7,0.5,5 ; 2,1,4,1 ; 1,6,7,0.5],
A = 3×4
5.0000 7.0000 0.5000 5.0000 2.0000 1.0000 4.0000 1.0000 1.0000 6.0000 7.0000 0.5000
[value,index]=min(A(:))
value = 0.5000
index = 7
[Iindex,Jindex]=ind2sub(size(A),index)
Iindex = 1
Jindex = 3

その他の回答 (2 件)

Stephen23
Stephen23 2022 年 5 月 19 日
A = [5,7,0.5,5;2,0.5,4,1;0.5,6,7,9];
[V,X] = min(A.',[],'all');
[C,R] = ind2sub(size(A),X)
C = 3
R = 1

Mathieu NOE
Mathieu NOE 2022 年 5 月 19 日
hello
here you are my friend
A=[5,7,0.5,5 ; 2,0.5,4,1 ; 0.5,6,7,9]
[val,ind_linear] = min(A,[],'all','linear'); % M = MIN(X,[],'all') returns the smallest element of X.
% [M,I] = MIN(X,[],...,'linear') returns the linear indices of the
% minimum values in vector I.
ind_linear = min(ind_linear); % take only the first one in case there are multiple answers
[row,col] = ind2sub(size(A),ind_linear) % Convert from the linear index back to its row and column form.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by