Please help me understand the meaning of this [Amax,Bmax]=max(max(AB,[ ],2));

4 ビュー (過去 30 日間)
adi kul
adi kul 2017 年 5 月 31 日
回答済み: Jan 2017 年 5 月 31 日
Hello All, I am looking for meaning out of a code I am having. the line in the code is:
AB=[2:0.1:7];
[Amax,Bmax]=max(max(AB,[ ],2));
Please help me understand the meaning of this piece of code.
  2 件のコメント
Adam
Adam 2017 年 5 月 31 日
編集済み: Adam 2017 年 5 月 31 日
It is basically nonsense code. The documentation explains the input and output arguments of the max function though
doc max
Following it through logically will show that the output for that example is meaningless.
Well, the first argument, Amax, does give you the maximum element of your array, but you can get that from simply
max( AB );
The second argument as it is used with a double usage of max on a vector array is meaningless and will always be 1. If AB was a proper 2d array then it would make some more sense and Bmax (a misleading name) would give you the row on which the maximum element of the matrix occurs.
Stephen23
Stephen23 2017 年 5 月 31 日
Also the square brackets are superfluous, and just make the code slower and more confusing:
AB=[2:0.1:7];
Better:
AB = 2:0.1:7;

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 5 月 31 日
Did you read the documentation already? The code is very basic, so reading the "Getting Started" chapters is recommended. The forum cannot replace this.
AB = [2:0.1:7];
is the same as:
AB = 2:0.1:7;
The square brackets are the concatenation operator, but 2:0.1:7 is a vector already: It is the row vector starting at 2 and going in steps of 0.1 until 7.
[Amax,Bmax]=max(max(AB,[ ],2));
From the inside to the outside:
max(AB,[ ],2)
This replies the maximum value of the vector AB along the 2nd dimension. Because AB is a row vector, you can omit the dim and write:
m = max(AB)
The next
[Amax,Bmax] = max(m);
is useless: Amax is the maximum value of m, but while m is a scalar, Amax will equal m. Bmax is the index of the largest element, and for a scalar input you get 1 in every case.
Perhaps this is meant:
[maxValue, maxIndex] = max(AB)
Note: If you proceed to post strange and very basic code in the forum and ask for the details instead of reading the documentation, the forum will loose the interest in solving your problems.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by