How to find the index of the minimum value in a matrix.
0 个评论
回答(5 个)
4 个评论
5 个评论
0 个评论
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 个评论
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.
0 个评论
另请参阅
类别
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!