Doubt regarding max(A,B).

2 views (last 30 days)
S Priya on 21 Aug 2021
Commented: Steven Lord on 21 Aug 2021
A=[1 3 5]
B=[10 6 4]
Why is max(A,B) as [10 6 5]?
It should be [5 10]
0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

Answers (2)

Stephen23 on 21 Aug 2021
Edited: Stephen23 on 21 Aug 2021
"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 CommentShow NoneHide None
Steven Lord on 21 Aug 2021
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

Sign in to comment.

VBBV on 21 Aug 2021
```%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 CommentsShow 5 older commentsHide 5 older comments
VBBV on 21 Aug 2021
For the OP.

Sign in to comment.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!