How to Find First Min Value And Index it??

5 views (last 30 days)
Nam Tran
Nam Tran on 2 Sep 2016
Commented: Fangjun Jiang on 6 Sep 2016
Hello, I have a matrix for example looks like this
q=[5 8 1
4 1 0
2 3 1]
minq=min(q(:))
I want to index it so something like [row col]=find(q==minq)
the problem is that this will index all of the 1's. I just want it to index it the first 1. keep in mind this is just an example and I am actually working with hundreds of thousands of grid blocks and this is the only way I can get the solution to converge. Thank you so much!

Answers (1)

Fangjun Jiang
Fangjun Jiang on 2 Sep 2016
Edited: Fangjun Jiang on 2 Sep 2016
[row col]=find(q==minq,1,'first')
or
q=[5 8 1
4 1 2
2 3 1]
[minq,Loc]=min(q(:));
[row col]=ind2sub(size(q),Loc)
  2 Comments
Nam Tran
Nam Tran on 2 Sep 2016
Thank you for your help! May I ask another questions? If I wanted to do a for loop, so that on the second loop, it indexes the second 1, how would i go about doing so? Thank you for your help
Fangjun Jiang
Fangjun Jiang on 6 Sep 2016
If you want the second, third index, then use your original method, [row col]=find(q==minq). The returned "row" and "col" are vectors containing all the index. You can loop through the elements of "row" and "col".

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!