How to find the min values and their indices for a 2D array?
157 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ilyes louahem m'sabah
el 11 de Sept. de 2023
Comentada: Adam Danz
el 12 de Sept. de 2023
I have the following 10x5 array: LLP
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1479381/image.png)
I want to get the min value of this array and all its induces. I mean (4,5), (5,1), (5,2)....and so on
0 comentarios
Respuesta aceptada
Adam Danz
el 11 de Sept. de 2023
Editada: Adam Danz
el 11 de Sept. de 2023
Find the minimum value using min and specify all dimensions. Then use find with two outputs to find the row and column indices of the matrix that equal the minimum value.
% demo data
m = [3 0 1 0 2; 2 2 0 0 3]
minval = min(m,[],'all');
[row, col] = find(m == minval)
Common pitfalls
min() has a second output that returns the indicies of the min value but, by default, it will return the row index of the first matching minimum value for each column.
[minval, idx] = min(m)
You can specify the linear option to return the linear index rather than a row index but this will still return the first matching minimum for each column, not all matches of the global minimum.
[minval, idx] = min(m,[],'linear')
If you specify all dimensions, it will return the first match of the minimum value, with or without the linear option.
[minval, idx] = min(m,[],'all')
5 comentarios
Adam Danz
el 12 de Sept. de 2023
m = [3 0 1 0 2; 2 2 0 0 3]
minval = min(m,[],'all');
[row, col] = find(m == minval)
f = rand(size(m))
ind = sub2ind(size(m),row,col);
f(ind)
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!