MATLAB Answers

Find maximum of all rows without a for-loop?

4 views (last 30 days)
buhmatlab
buhmatlab on 15 Apr 2020
Commented: buhmatlab on 15 Apr 2020
Hi,
I am relatively new to Matlab and only used to work with for-loops which I've already used succesfully to find the maximum in each row of my three column vectors (300X1):
for i=1:length(value_A)
test(i) = max([value_A(i),value_B(i),value_C(i)]);
end
Since this is not a very elegant and efficient way (for loop) I'm wondering how I can achieve the same result without a for loop.
Many thank in advance!

  2 Comments

buhmatlab
buhmatlab on 15 Apr 2020
Sorry, my bad: column vector is correct

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 15 Apr 2020
Edited: Stephen Cobeldick on 15 Apr 2020
Assuming that your three vectors have size 1xN (i.e. row vectors):
M = [value_A;value_B;value_C];
T = max(M,[],1)
Assuming that your three vectors have size Nx1 (i.e. column vectors):
M = [value_A,value_B,value_C];
T = max(M,[],2)
For vectors of any orientation:
M = [value_A(:),value_B(:),value_C(:)];
T = max(M,[],2)

  1 Comment

buhmatlab
buhmatlab on 15 Apr 2020
Thank you! Sorry I meant column vector, edited the original question.

Sign in to comment.

More Answers (0)


Translated by