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

 採用された回答

Mischa Kim
Mischa Kim 2017 年 11 月 7 日
編集済み: Mischa Kim 2017 年 11 月 7 日
Use
[val,loc] = min(R')
val =
21 35 42 % min value in each row
loc =
3 1 1 % column location of min value

4 件のコメント

thank you for you help, but how can I access 1 row at a time?
[val,loc] = min(R(1,:)) % min value in 1st row
val =
21
loc =
3
Replace the "1" in the min command by a running index to step through each row, e.g., using a loop.
thank you
suppose a matrix R = ones(4,4)
Every element is maximum and minimum, how to extract their indices?
How to extract the indices of max or min if there are more than 1 in col and in row?

サインインしてコメントする。

その他の回答 (1 件)

Ariunbolor Purvee
Ariunbolor Purvee 2020 年 8 月 6 日
編集済み: Ariunbolor Purvee 2020 年 8 月 6 日
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 件のコメント

Using sum as a variable is not a good idea, sum() is an in-built function.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by