how to find min or max value in each row of a matrix and its index, if a matrix is 3x2 it will find the min in first row
Show older comments
R= [27 25 21; 35 38 37; 42 47 49]; 1st min value will be 21
Accepted Answer
More Answers (1)
Ariunbolor Purvee
on 6 Aug 2020
Edited: Ariunbolor Purvee
on 6 Aug 2020
0 votes
absDiff=[1 2 3; 4 5 6; 7 8 9; 9 10 12];
MaxMinAveRow= MaxMinAveOfRow(absDiff);
display(MaxMinAveRow);
function MaxMinAveRow= MaxMinAveOfRow(absDiff)
n=length(absDiff);
maxRow=zeros(n,1);
minRow=zeros(n,1);
aveRow=zeros(n,1);
sum=0;
for i=1:n
maxRow(i)=sum+max(absDiff(i,:));% max of row
minRow(i)=sum+min(absDiff(i,:));% min of row
aveRow(i)=sum+mean(absDiff(i,:));% average of row
end
MaxMinAveRow=[ maxRow,minRow, aveRow ];
end
The result on Command Window
MaxMinAveRow =
3.0000 1.0000 2.0000
6.0000 4.0000 5.0000
9.0000 7.0000 8.0000
12.0000 9.0000 10.3333
1 Comment
madhan ravi
on 6 Aug 2020
Using sum as a variable is not a good idea, sum() is an in-built function.
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!