Finding the maximum of rows
258 ビュー (過去 30 日間)
古いコメントを表示
Say that we have the following matrix:
I=[3 4; 5 3; 6 3; 7 4];
If we want to find the maximum value in each row, we can do the following:
m=max(I,[],2);
For m, how do we read this? How is the statement interpreted? What should we do if we want to find the maxim of the columns?
Thanks.
2 件のコメント
Wesley Brigner
2022 年 5 月 17 日
There are probably multiple ways to find the max of every other row, but the simplest I can think of is to just use matrix indexing:
i=1; % Changes the starting row. Can be either 1 or 2.
m=max(I(i:2:end,:),[],2);
Of course, this can be generalized if you want to take every nth row:
i=1; % Changes the starting row. Positive integer between 1 and n.
n=4; % Changes the increment. Positive integer.
m=max(I(i:n:end,:),[],2);
回答 (2 件)
Wayne King
2014 年 1 月 1 日
編集済み: Wayne King
2014 年 1 月 1 日
[m,idx] = max(I,[],2);
m gives you the maximum value in each row and idx gives you the column in which it occurs. I used this in my other response to you about fcm()
To find the maximum of the columns, just operate along the rows
[m,idx] = max(I,[],1);
3 件のコメント
Wayne King
2014 年 1 月 1 日
Are you reading the documentation?? The syntax max(x,y) takes the maximum of x or y, so if you just called:
max(I,1)
that would not give you what you want. You want 1 or 2 to represent the dimension along which you want the maximum and that has to be the third input argument.
Walter Roberson
2014 年 1 月 1 日
max(A) means the maximum of A column-wise
max(A,B) means the maximum element-by-element of A(I,J) vs B(I,J)
max(A,[],k) means the maximum of A along the k'th dimension. You need the [] as a place-holder so that MATLAB does not confuse this with max(A,B)
Ariunbolor Purvee
2020 年 8 月 6 日
編集済み: Ariunbolor Purvee
2020 年 8 月 6 日
clc;clear
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!