Finding the maximum of rows

조회 수: 258 (최근 30일)
med-sweng
med-sweng 2014년 1월 1일
댓글: Wesley Brigner 2022년 5월 17일
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
Jonathan Timberlake 2021년 9월 16일
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일
편집: 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