How to find the index of the minimum value in a matrix.

조회 수: 373 (최근 30일)
Micaiah Barletta
Micaiah Barletta 2022년 1월 19일
답변: Steven Lord 2022년 1월 20일
Hi everyone,
So I have a 9x21 matrix, V, and I am trying to find the index (i,j) of the minimum (closest to zero) value in that matrix. I have tried using several forms of the min function, but it keeps returning multiple indices for the the minimum values in each column. I only want one index (i,j), that of the minimum value in the ENTIRE matrix.
How would I go about doing this? Thanks!

답변 (3개)

Stephen23
Stephen23 2022년 1월 19일
편집: Stephen23 2022년 1월 19일
Where M is your matrix:
[R,C] = find(M==min(M(:)))
or
[~,X] = min(M(:)); % or [~,X] = min(M,[],'all','linear');
[R,C] = ind2sub(size(M),X)
  댓글 수: 3
Micaiah Barletta
Micaiah Barletta 2022년 1월 19일
Oh, wait, so I wanted the value that was closest to zero, but instead this command is giving me the value that is the smallest.
Image Analyst
Image Analyst 2022년 1월 19일
Try
% Sample data between -2.5 and +2.5
M = 5 * rand(5, 10) - 2.5
M = 5×10
2.2558 2.0285 0.7944 2.3343 0.7202 0.8739 -0.0449 -1.9158 -1.0654 -1.8531 -0.9877 1.7554 2.4745 -2.4179 0.1576 1.4184 0.2105 1.1018 2.1986 -2.3464 0.0450 2.3143 -0.8332 2.0228 0.1946 -0.5806 -0.5764 1.6439 -1.6989 1.2254 -1.1251 -0.8863 0.5182 -0.3079 -2.0536 -0.3036 1.9644 -1.4574 -0.9964 1.8352 -0.9463 -0.9253 0.9574 -0.8925 0.3358 1.1363 -2.4485 -2.0140 0.0612 0.1545
% Find value closest to 0:
minValue = min(abs(M(:)))
minValue = 0.0449
[r, c] = find(abs(M) == minValue)
r = 1
c = 7

댓글을 달려면 로그인하십시오.


Yusuf Suer Erdem
Yusuf Suer Erdem 2022년 1월 19일
Hi, did you try it with this way. It gives the index which has the smallest number.
a=[8 2 5 1;2 -2 4 0;9 5 4 8];
[r,c]=find(a==min(a(:)))
  댓글 수: 5
Yusuf Suer Erdem
Yusuf Suer Erdem 2022년 1월 19일
I'll take a look when I arrive to home.
Yusuf Suer Erdem
Yusuf Suer Erdem 2022년 1월 20일
Hi, I strived a lot on your codes but I finally achieved it. The codes which are below works. I am glad if you accept my answer;
clc; clear;
a = rand(9,21);
b= 0;
differences = abs(a-b)
minDiff = min(differences);
closestValue = min(minDiff);
[r c]=find(a==closestValue)

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2022년 1월 20일
Since you're using release R2021b (according to the information listed on the right side of this page) you can use 'all' as the dimension input and specify both the ComparisonMethod parameter and the 'linear' option.
M = randn(6)
M = 6×6
0.9423 0.3336 0.1760 1.0120 1.5974 -0.7014 -2.2171 -0.4520 -0.2327 -0.8261 -0.9182 -0.8860 0.7431 -0.7179 -0.2381 0.0671 0.9823 -1.0860 1.6596 -0.9416 -0.0795 -0.2284 -1.1704 -0.8441 -0.4919 -0.3952 0.6310 2.4187 -0.0775 0.2292 0.1780 0.3546 -0.3958 0.1260 -0.0577 0.7462
[minValue, minIndex] = min(M, [], 'all', 'linear', 'ComparisonMethod', 'abs')
minValue = -0.0577
minIndex = 30
For this particular random matrix, the entry with the smallest absolute value is the element with linear index 30.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by