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

388 ビュー (過去 30 日間)
Micaiah Barletta 2022 年 1 月 19 日

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 2022 年 1 月 19 日

[R,C] = find(M==min(M(:)))
or
[~,X] = min(M(:)); % or [~,X] = min(M,[],'all','linear');
[R,C] = ind2sub(size(M),X)
##### 3 件のコメント表示非表示 2 件の古いコメント
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 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 件のコメント表示非表示 4 件の古いコメント
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)
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.

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

### カテゴリ

Find more on Matrix Indexing in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by