How to find the 5 minimum values in a multidimensional matrix and the indices to which these entries correspond.

26 次查看(过去 30 天)
I have a 597 x 194 matrix to which I need to find the 5 smallest values within the entire matrix. As well as this I need the corresponding indices for those minimum values.
i.e. the smallest value (of the 5) is value 4.1146, and corresponds to indices of 336, 170. How could I find this value along with the next 4 smallest, and the indices for the entry?
  2 个评论
Robin
Robin 2022-12-23
To find the 5 smallest values in a matrix and their corresponding indices, you can use the min function along with the sort and ind2sub functions in MATLAB. Here is an example of how to do this:
%
% Define the matrix
% (this is terrible, since you are likely to get multiple
% zeros as random values over the 115818 element matrix
A = rand(597, 194);
% Find the 5 smallest values and their indices
[sortedValues, sortedIndices] = sort(A(:));
smallestValues = sortedValues(1:5);
smallestIndices = sortedIndices(1:5);
% Convert the linear indices to row-column indices
[rowIndices, colIndices] = ind2sub(size(A), smallestIndices);
% Print the results
fprintf('Smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(1), rowIndices(1), colIndices(1));
Smallest value: 0.0000, indices: (436, 97)
fprintf('Second smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(2), rowIndices(2), colIndices(2));
Second smallest value: 0.0000, indices: (582, 173)
fprintf('Third smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(3), rowIndices(3), colIndices(3));
Third smallest value: 0.0000, indices: (579, 73)
fprintf('Fourth smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(4), rowIndices(4), colIndices(4));
Fourth smallest value: 0.0000, indices: (578, 119)
fprintf('Fifth smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(5), rowIndices(5), colIndices(5));
Fifth smallest value: 0.0001, indices: (175, 30)
This code first flattens the matrix into a vector using the : operator and then sorts it using the sort function. It then selects the first 5 values and their indices and converts the linear indices to row-column indices using the ind2sub function. Finally, it prints the results to the command window.
I hope this helps! Let me know if you have any questions.
Charles Howman
Charles Howman 2022-12-23
编辑:Charles Howman 2022-12-23
Yes this is perfect thank you! It is a much more elegant solution to one one I eventually went with which involved using min to find the smallest value, then re-assigning it to a larger value.
%initialise matrix to store values
minimums = zeros(5, 3);
%start by finding smallest value in whole matrix
[pairDmin1] = min(pairD, [],'all');
minimums(1, 1) = pairDmin1;
%then find the indices for this matrix
[X1, Y1] = find(pairD==pairDmin1);
minimums(1, 2) = X1;
minimums(1, 3) = Y1;
%now going to replace this value by a large value
pairD(X1, Y1) = 999;
%now find new smallest value which will be 2nd overall smallest
[pairDmin2] = min(pairD, [], 'all');
minimums(2, 1) = pairDmin2;
[X2, Y2] = find(pairD==pairDmin2);
minimums(2, 2) = X2;
minimums(2, 3) = Y2;
pairD(X2, Y2) = 999;
%now 3rd smallest
[pairDmin3] = min(pairD, [], 'all');
minimums(3, 1) = pairDmin3;
[X3, Y3] = find(pairD==pairDmin3);
minimums(3, 2) = X3;
minimums(3, 3) = Y3;
pairD(X3, Y3) = 999;
%now 4th smallest
[pairDmin4] = min(pairD, [], 'all');
minimums(4, 1) = pairDmin4;
[X4, Y4] = find(pairD==pairDmin4);
minimums(4, 2) = X4;
minimums(4, 3) = Y4;
pairD(X4, Y4) = 999;
%now 5th smallest
[pairDmin5] = min(pairD, [], 'all');
minimums(5, 1) = pairDmin5;
[X5, Y5] = find(pairD==pairDmin5);
minimums(5, 2) = X5;
minimums(5, 3) = Y5;
pairD(X5, Y5) = 999;
%now can view the 5 minimums and their corresponding indices
display(minimums)

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2022-12-23
% Some input data
rng default
M = rand(3);
% Find the sorted values of M, and the linear matrix indexing to them
[sortedM,linearIndex] = sort(M(:));
% Find the row and column indices
[rowIndex,columnIndex] = ind2sub(size(M),linearIndex);
% The first 5 of them
n = 5;
sortedM(1:n)
ans = 5×1
0.0975 0.1270 0.2785 0.5469 0.6324
rowIndex(1:n)
ans = 5×1
3 3 1 2 2
columnIndex(1:n)
ans = 5×1
2 1 3 3 2

更多回答(1 个)

William Rose
William Rose 2022-12-23
A=rand(3,4)
A = 3×4
0.4651 0.6844 0.3092 0.4551 0.9791 0.6301 0.3823 0.2112 0.9126 0.7742 0.4814 0.3253
[r,c]=size(A);
k=5; %number of values to find
[minval,idx]=mink(reshape(A,[],1),k);
minrow=mod(idx,r);
minrow(minrow==0)=3;
mincol=ceil(idx/r);
disp([minval,minrow,mincol])
0.2112 2.0000 4.0000 0.3092 1.0000 3.0000 0.3253 3.0000 4.0000 0.3823 2.0000 3.0000 0.4551 1.0000 4.0000
Try it. Good luck.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by