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

30 次查看(过去 30 天)
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.

回答(2 个)

Star Strider
Star Strider 2016-9-6
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 2016-9-6
编辑:George 2016-9-6
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

类别

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