How to find the 5 minimum values in a multidimensional matrix and the indices to which these entries correspond.
26 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Charles Howman
le 23 Déc 2022
Modifié(e) : Charles Howman
le 23 Déc 2022
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 commentaires
Robin
le 23 Déc 2022
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));
fprintf('Second smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(2), rowIndices(2), colIndices(2));
fprintf('Third smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(3), rowIndices(3), colIndices(3));
fprintf('Fourth smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(4), rowIndices(4), colIndices(4));
fprintf('Fifth smallest value: %.4f, indices: (%d, %d)\n', ...
smallestValues(5), rowIndices(5), colIndices(5));
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.
Réponse acceptée
the cyclist
le 23 Déc 2022
% 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)
rowIndex(1:n)
columnIndex(1:n)
Plus de réponses (1)
William Rose
le 23 Déc 2022
A=rand(3,4)
[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])
Try it. Good luck.
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!