How to find the index of the minimum value in a matrix.
조회 수: 373 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (3개)
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
Image Analyst
2022년 1월 19일
Try
% Sample data between -2.5 and +2.5
M = 5 * rand(5, 10) - 2.5
% Find value closest to 0:
minValue = min(abs(M(:)))
[r, c] = find(abs(M) == minValue)
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
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
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)
[minValue, minIndex] = min(M, [], 'all', 'linear', 'ComparisonMethod', 'abs')
For this particular random matrix, the entry with the smallest absolute value is the element with linear index 30.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!