Finding the maximum of rows

231 Ansichten (letzte 30 Tage)
med-sweng
med-sweng am 1 Jan. 2014
Kommentiert: Wesley Brigner am 17 Mai 2022
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 Kommentare
Jonathan Timberlake
Jonathan Timberlake am 16 Sep. 2021
HI how would you take the max of every other row?
Wesley Brigner
Wesley Brigner am 17 Mai 2022
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);

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Wayne King
Wayne King am 1 Jan. 2014
Bearbeitet: Wayne King am 1 Jan. 2014
[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 Kommentare
Wayne King
Wayne King am 1 Jan. 2014
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 am 1 Jan. 2014
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)

Melden Sie sich an, um zu kommentieren.


Ariunbolor Purvee
Ariunbolor Purvee am 6 Aug. 2020
Bearbeitet: Ariunbolor Purvee am 6 Aug. 2020
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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by