min function on two arrays

조회 수: 49 (최근 30일)
Robert Demyanovich
Robert Demyanovich 2021년 6월 8일
댓글: Walter Roberson 2021년 6월 8일
I've read the documentation on the min function, but still don't understand how it works on two arrays. I have the following:
dS=zeros(1,N)
dS=min(cA(i+1,:),cB(i+1,:))
where cA and cB are equally sized arrays. Doesn't the min function just take the value at equivalent locations in cA or cB that is the lowest. So if position 10,50 in cA is 5 and the same position in cB is 3, min returns 3 in the resulting row vector, dS, at column 50. Is that correct?
  댓글 수: 3
Robert Demyanovich
Robert Demyanovich 2021년 6월 8일
No, I'm just a MatLab beginner trying to understand some complex (for me, anyway) code.
What if dS had been defined as an array? I guess the result is the same because in both cA and cB, the i+1 row has been specified.
James Tursa
James Tursa 2021년 6월 8일
편집: James Tursa 2021년 6월 8일
This line creates dS as a vector:
dS=zeros(1,N)
Then this line completely overwrites the dS you just created and instead assigns dS the result of the min( ) function call:
dS=min(cA(i+1,:),cB(i+1,:))
I.e., the first line is completely useless and accomplishes nothing because it gets overwritten in the second line.
And yes, cA(i:1,:) and cB(i+1,:) will be a row vectors if i is a scalar.

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

채택된 답변

SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 8일
%
dS=min([cA(i+1,:),cB(i+1,:)])
  댓글 수: 3
SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 8일
Oops, yes it must be semicol; Thanks.
% correction
dS=min([cA(i+1,:);cB(i+1,:)])
Walter Roberson
Walter Roberson 2021년 6월 8일
That would work in the case that cA and cB have the same number of columns.
The original code
dS=min(cA(i+1,:),cB(i+1,:))
would also work if one of cA or cB had a single column and the other one did not.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by