How to find multiple min values and index them for a FOR loop

75 visualizaciones (últimos 30 días)
Nam Tran
Nam Tran el 6 de Sept. de 2016
Editada: George el 6 de Sept. de 2016
I am working with a very large matrix (141x141). In this matrix are multiple min values. Right now my code finds the first minimum value and indexes the row and col to further be used in the code. In the next iteration, I want the code to find the next minimum value, which by coincidence has the same min value and index it to run for the code. But the problem is that it keeps indexing that first min value. Is there a way to write a code that says "find minimum value and index it, if the index has already been used, use the next index that has the same value." I'd like to do this without doing a for loop, since I already have a for loop for the calculations, implementing a for loop to solve this problem would make things very complicated.

Respuestas (2)

Star Strider
Star Strider el 6 de Sept. de 2016
Use the find funciton to locate all of them. I prefer to use it with linear indexing instead of getting the row and column indices.
Example:
A = randi(99, 141); % Create Data
Amins = find(A(:) == min(A(:))); % Find All Minima
This approach uses linear indexing. See the section on Linear Indexing in the Matrix Indexing documentation for details on how it works.

George
George el 6 de Sept. de 2016
Editada: George el 6 de Sept. de 2016
The first thing I would do is find what the minimum value is. You can do this by using the colon notation
A
A =
17 0 1 8 15
0 5 7 14 16
4 6 13 20 22
0 0 0 21 3
11 18 25 2 9
>> m = min(A(:));
Then, you can find all instances where A==m.
>> find(A==m)
ans =
2
4
6
9
14
Those are the logical indicies of the minimum elements

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by