A=[1 3 5]
B=[10 6 4]
Why is max(A,B) as [10 6 5]?
It should be [5 10]

답변 (2개)

Stephen23
Stephen23 2021년 8월 21일
편집: Stephen23 2021년 8월 21일

3 개 추천

"Why is max(A,B) as [10 6 5]?"
Because that syntax provides an element-wise comparison of the two input arrays, taking the maximum value from either one of the input arrays:
  1. the maximum of 1 and 10 is 10
  2. the maximum of 3 and 6 is 6
  3. the maximum of 5 and 4 is 5
"It should be [5 10]"
No, it should not be.
What are are expecting is the maximum of each row of one matrix (not comparing the elements of two different arrays):
max([A;B],[],2)
ans = 2×1
5 10
Note that in some other languages the two functionalities are considered as different functions, e.g. in numpy they are amax and maximum respectively.

댓글 수: 1

I suspect the original poster's mental model may have been that max(A, B) is [max(A), max(B)] and max(A, B, C, ...) is the same as [max(A), max(B), max(C), ...]. But as many people have said in this discussion, that is incorrect. If A and B are the same size each element of the output of max(A, B) is the maximum of the corresponding elements of A and B.
A=[1 3 5];
B=[10 6 4];
C_allAtOnce = max(A, B)
C_allAtOnce = 1×3
10 6 5
C_oneAtATime = [max(A(1), B(1)), max(A(2), B(2)), max(A(3), B(3))]
C_oneAtATime = 1×3
10 6 5
Compare with:
maxOfEachVector = [max(A), max(B)]
maxOfEachVector = 1×2
5 10

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

VBBV
VBBV 2021년 8월 21일

1 개 추천

%f true
max([A B])
max([A;B])

The first would give 10 as max value. The second would give 10 6 5 as max values along each column

댓글 수: 6

S Priya
S Priya 2021년 8월 21일
Thank you
VBBV
VBBV 2021년 8월 21일
편집: VBBV 2021년 8월 21일
%if true
max(A,B)
By default MATLAB assumes column as 1st dimension. So above usage also gives same result as 10 6 5
Stephen23
Stephen23 2021년 8월 21일
편집: Stephen23 2021년 8월 21일
"max(A,B) By default max uses column as 1st dimension. So above usage also gives same result as 10 6 5"
This is incorrect.
Do NOT learn from this incorrect comment.
The default direction is only relevant when one input array only is provided (i.e. when the maximum is taken along any dimension of one array), not when comparing the elements of two arrays. With the syntax shown in the question, the default direction is totally irrelevant.
Even for just one input array the default direction is not the "column as 1st dimension", but is actually the first non-scalar dimension.
This explanation indicates a major confusion of how MAX and MIN syntaxes work.
VBBV
VBBV 2021년 8월 21일
read the doc page of max for more clarity
Stephen23
Stephen23 2021년 8월 21일
편집: Stephen23 2021년 8월 21일
"read the doc page of max for more clarity"
The english is unlcear: is that is an imperative (for me? for the OP?) or past tense of what you have done.
Is this intended to replace your incorrect advice regarding the MAX/MIN syntax with two input arrays?
VBBV
VBBV 2021년 8월 21일
For the OP.

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

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

질문:

2021년 8월 21일

댓글:

2021년 8월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by