Clear Filters
Clear Filters

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

501 views (last 30 days)
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!

Answers (5)

Stephen23
Stephen23 on 19 Jan 2022
Edited: Stephen23 on 19 Jan 2022
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)
  4 Comments
Image Analyst
Image Analyst on 19 Jan 2022
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
Umar
Umar on 7 Aug 2024
Hi Micaiah Barletta,
When you mentioned , “ I wanted the value that was closest to zero”, Hope this is what you meant
% Define a 9x21 matrix with manual values V = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21; 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42; 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63; 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84; 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105; 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126; 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147; 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168; 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189];
% Display the matrix
disp('The 9x21 matrix V with manual values:');
disp(V);
% Calculate the absolute differences between each element and zero
abs_diff = abs(V - 0);
% Find the linear index of the element with the minimum absolute difference
[~, linear_index] = min(abs_diff(:));
[i, j] = ind2sub(size(V), linear_index);
disp('Linear index of the value closest to zero:');
disp(linear_index);
disp('Row and column indices of the value closest to zero:');
disp([i, j]);
Please let us know if you have any further questions.

Sign in to comment.


Yusuf Suer Erdem
Yusuf Suer Erdem on 19 Jan 2022
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 Comments
Yusuf Suer Erdem
Yusuf Suer Erdem on 20 Jan 2022
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)

Sign in to comment.


Steven Lord
Steven Lord on 20 Jan 2022
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.

Umar
Umar on 7 Aug 2024
Edited: Umar on 7 Aug 2024

Hi @Micaiah Barletta,

To address your comments about, “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?”

First, I will defined the matrix 'V' as a 9x21 matrix by using the 'randn' function for demonstration purposes. Note: please replace this with your actual matrix data. Then, I use the min function to find the minimum value in the entire matrix 'V'. Afterwards, I employed find function to locate the linear index of the minimum value. Finally, the ind2sub function is utilized to convert the linear index to the corresponding row and column indices.

% Define matrix V (9x21)

V = randn(9, 21); % Example: Generating a random matrix for demonstration

disp('Matrix V:');

disp(V);

% Find the minimum value in the entire matrix

min_value = min(V(:));

disp('Minimum value in matrix V:');

disp(min_value);

% Find the linear index of the minimum value

[min_row, min_col] = find(V == min_value, 1, 'first');

disp('Linear index of the minimum value:');

disp([min_row, min_col]);

% Convert linear index to row and column indices

[i, j] = ind2sub(size(V), min_row);

disp('Row and column indices of the minimum value:');

disp([i, j]);

Please see attached.

Hope this helps. Please let me know if you have any further questions.

  2 Comments
Stephen23
Stephen23 on 7 Aug 2024
"Afterwards, I employed find function to locate the linear index of the minimum value."
[min_row, min_col] = find(V == min_value, 1, 'first'); % the outputs are NOT linear indices
MIN_ROW is not a linear index. It is the row subscript index.
MIN_COL is not a linear index. It is the column subscript index.
The part of the code commented "Find the linear index of the minimum value" does not find the linear index of the minimum value. It finds the row and column subscript indices of the minimum value.
The part of the code commented "Convert linear index to row and column indices" does not convert a linear index, because there is no linear index anywhere in any of that code:
[i, j] = ind2sub(size(V), min_row); % 2nd input is NOT a linear index
"Finally, the ind2sub function is utilized to convert the linear index to the corresponding row and column indices."
A simple sanity check shows that the last part also does not work as described: the claim is that the minimum value is at row=2 and column=1. Here is (apparently) the minimum value shown in the author's own screenshot:
It is clear that its location is nowhere near row=2 column=1.
Umar
Umar on 7 Aug 2024
Hi @Stephen23,
I highly appreciate your input and thorough analysis of my code. I did realize after reading your comments that it provided attempts to find the minimum value in the matrix V and its corresponding row and column indices and misconception of the usage of linear indexing. So, after reviewing everyone’s comments on this post and OP’s comments again, the proposed solution is what you suggested earlier which is simply finding the linear index of the minimum value in the entire matrix by directly using the min function with linear indexing (V(:)).

Sign in to comment.


Umar
Umar on 7 Aug 2024

Hi @ Micaiah Barletta,

Since you have not click “Answer Accepted”, please see revised code below.

% Define a 9x21 matrix with manual values

V = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21; 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42; 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63; 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84; 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105; 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126; 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147; 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168; 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189];

% Display the matrix

disp('The 9x21 matrix V with manual values:');

disp(V);

% Find the linear index of the minimum value in the entire matrix

[~, linear_index] = min(V(:));

[i, j] = ind2sub(size(V), linear_index);

disp('Linear index of the minimum value:');

disp(linear_index);

disp('Row and column indices of the minimum value:');

disp([i, j]);

Please see attached results.

Hope this helps.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!