What does [m,i] = max(u) mean in the question

3 ビュー (過去 30 日間)
basim almutairi
basim almutairi 2021 年 8 月 29 日
回答済み: Walter Roberson 2021 年 8 月 29 日
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

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 8 月 29 日
[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)
m = 1×1×0×5 empty double array i = 1×1×0×5 empty double array
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)
m =
m(:,:,1) = 0.9527 m(:,:,2) = 0.9078 m(:,:,3) = 0.9250
i =
i(:,:,1) = 6 i(:,:,2) = 4 i(:,:,3) = 2
size(m)
ans = 1×3
1 1 3
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.

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by