finding min matrix and where that is
Show older comments
Hi all I have a matrix, I want to find its minimum and where that minimum is, can anybody help me with that?
Answers (1)
Star Strider
on 10 Aug 2016
This works to find the first minimum:
M = randi(9, 5); % Create Matrix
[Mmin,lidx] = min(M(:));
[r,c] = ind2sub(size(M),lidx);
Result = [Mmin r c]
If there are mor than one values of the minimum:
M = randi(9, 5); % Create Matrix
Mmin = min(M(:))
[r,c] = find(M == Mmin)
3 Comments
bachelor student
on 10 Aug 2016
bachelor student
on 10 Aug 2016
Star Strider
on 10 Aug 2016
My pleasure.
To get the minimum value and locations for all values not equal to zero, this works:
M = randi([0 5], 5); % Create Matrix
Mmin = min(M(M(:) ~= 0))
[r,c] = find(M == Mmin)
(This also allows for negative values in the matrix if necessary, even though the test matrix values go from 0 to 5 here.)
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!