Finding the maximum of rows

257 次查看(过去 30 天)
med-sweng
med-sweng 2014-1-1
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 个评论
Jonathan Timberlake
HI how would you take the max of every other row?
Wesley Brigner
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
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
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
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
编辑: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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by