Minimum of a matrix with zeros
3 views (last 30 days)
Show older comments
See in attachment the figure of my matrix. I can see that the number '340' is the lowest in my matrix (except for zero). I want to know the rownumber and columnnumber of 340 (ofcourse without going to check what the location of 340 is). Help?
0 Comments
Accepted Answer
Image Analyst
on 27 Dec 2014
A trick: how about setting 0's to inf:
m(m==0) = inf;
Then using min() to get the index of the min value. I'll let you do that since it's homework . Look at both return arguments of min().
3 Comments
Star Strider
on 27 Dec 2014
The ‘maxNum’ value is the value of the result returned by the min funciton (the value of the minimum here), and ‘maxIndex’ is the index of the value in the array. Note that here, ‘Hoogste_US_matrix(:)’ has been converted to a column vector (the (:) subscript notation does that). You would have to use the ind2sub function to convert that index back to the appropriate matrix subscripts.
Image Analyst
on 27 Dec 2014
I'd call them minValue instead of maxNum, and linearIndexOfMin instead of maxIndex. They are mins after all, not maxes.
minValue (what you called maxNum for some reason) is the minimum value in the array, which will no longer be zero since we replaced zeros with infinity.
The linear index is what number you'd get if you counted over to the location of the min by going down the columns from left to right. For example for a 3 by 5 matrix, these would be the linear indexes:
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15
To get the actual row and column, you have to use ind2sub() as you already figured out.
If I helped you, please mark as "Accepted". Thanks in advance.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!