Given a 3 by 3 matrix or greater matrix. How to find the position of the matrix with minimum sum in the matrix
2 views (last 30 days)
Show older comments
Given a 3 by 3 matrix, how to find the position of the row with minimum sum, by indexing
2 Comments
Answers (2)
Image Analyst
on 13 Nov 2018
You can use this:
m = randi(9, 3, 3)
sums = sum(m, 2) % Get row sums (sum across columns within rows)
minSum = min(sums); % Find min sum value - may occur more than once.
rowsWithMinSum = find(sums == minSum)
For example, you might see this in the command window:
m =
1 8 2
4 4 3
3 9 2
sums =
11
11
14
rowsWithMinSum =
1
2
You need to do it like this rather than use the min function's second output because that will only find the first min, and if the min occurs more than once, it won't find the rest.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!