Finding the maximum of rows

258 visualizzazioni (ultimi 30 giorni)
med-sweng
med-sweng il 1 Gen 2014
Commentato: Wesley Brigner il 17 Mag 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 Commenti
Jonathan Timberlake
Jonathan Timberlake il 16 Set 2021
HI how would you take the max of every other row?
Wesley Brigner
Wesley Brigner il 17 Mag 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);

Accedi per commentare.

Risposte (2)

Wayne King
Wayne King il 1 Gen 2014
Modificato: Wayne King il 1 Gen 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 Commenti
Wayne King
Wayne King il 1 Gen 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 il 1 Gen 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)

Accedi per commentare.


Ariunbolor Purvee
Ariunbolor Purvee il 6 Ago 2020
Modificato: Ariunbolor Purvee il 6 Ago 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

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by