What does [m,i] = max(u) mean in the question
7 views (last 30 days)
Show older comments
Greetings all,
I have the following matrix u = [-1 5 -7 3] and I have the following command [m,i] = max(u) and the matlab gives me the answer of
i =
2
what does [m,i] mean and how did i get the 2
0 Comments
Answers (1)
Walter Roberson
on 29 Aug 2021
[m,i] = max(u)
starts by detecting the first "non-scalar" dimension in u -- the first dimension in which size(u) is not 1.
If all of the dimensions are 1 (a scalar) then the return value is the scalar value, and 1 in i
If any of the dimensions are 0, then the return value in m is any empty array the same size as u except with the first non-scalar dimension replaced with 1, and with i being empty.
u = rand(1, 7, 0, 5);
[m, i] = max(u)
Notice the first non-scalar dimension is the second dimension, and that dimension becomes scalar (1) in the output
If none of the dimensions are empty, then MATLAB takes the maximum along the first non-scalar dimension, returning a value corresponding to all remaining dimensions.
u = rand(1, 7, 3);
[m, i] = max(u)
size(m)
The first non-scalar dimension was the second one, so the m output becomes 1 x 1 by the remaining dimenions (which is x 3 in this case).
In each case the i output is the same size as the m output, and it tells you the index along the first non-scalar dimension at which the maximum was found. In the above example, the maximum of u(1,:,1) is at u(1,6,1) and the maximu of u(1,:,2) is at u(1,4,2) and the maximum of u(1,:,3) was at u(1,2,3), so the index outputs in u are 6, 4, 2, but arranged along an appropriate dimension.
In the case of your example, the 2 that is returned in i tells you that u(2) was the location of the maximum.
0 Comments
See Also
Categories
Find more on Structures 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!