Find maximum of all rows without a for-loop?

조회 수: 4 (최근 30일)
buhmatlab
buhmatlab 2020년 4월 15일
댓글: buhmatlab 2020년 4월 15일
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
Stephen23
Stephen23 2020년 4월 15일
편집: Stephen23 2020년 4월 15일
" ...three row vectors (300X1)"
A row vector has size 1xN
A column vector has size Nx1
buhmatlab
buhmatlab 2020년 4월 15일
Sorry, my bad: column vector is correct

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 4월 15일
편집: Stephen23 2020년 4월 15일
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
buhmatlab
buhmatlab 2020년 4월 15일
Thank you! Sorry I meant column vector, edited the original question.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by