How can I make multiple max in elegant way?

4 ビュー (過去 30 日間)
Eli Borodach
Eli Borodach 2016 年 3 月 10 日
コメント済み: Walter Roberson 2016 年 3 月 10 日
Hello all, let's assume I have 8 vectors x1,x2,x3,...,x8 I can use: max(max(max(x1,x2),x3),x4) and so on... But how can I do it in a more elegant way?
Thanks in Advance

採用された回答

Ced
Ced 2016 年 3 月 10 日
編集済み: Ced 2016 年 3 月 10 日
you could do
max(max([ x1 x2 x3 x4 x5 .... ]))
Since the first max is evaluated along the columns, I think it should be fast enough. Alternatively, you could of course do X = [ x1 x2 x3 ... ] and then max(X(:)). That's shorter, but depending on the length of your vectors, I would go with the first option. A common alternative is to use sort, e.g.
X_sorted = sort([ x1 x2 ... ]);
xmax = max(X_sorted(end,:))
but since for max, you don't need the matrix to be fully sorted, I would guess that max is actually faster.
  2 件のコメント
Eli Borodach
Eli Borodach 2016 年 3 月 10 日
Thanks you very much!
Walter Roberson
Walter Roberson 2016 年 3 月 10 日
Are you looking for the overall maximum among the 8 vectors? Or are you looking for the maximum for each entry in the vector -- e.g.,
[max([x1(1), x2(1), x3(1), x4(1), x5(1), x6(1), x7(1), x8(1)]),
max([x1(2), x2(2), x3(2), x4(2), x5(2), x6(2), x7(2), x8(2)]),
...
max([x1(end), x2(end), x3(end), x4(end), x5(end), x6(end), x7(end), x8(end)]) ];
Your max(max(max(...max(x1,x2), x3), x4 ... code is taking maximum for each entry, which is what I coded for. Some of the solutions Ced posted are for the overall maximum.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 3 月 10 日
If they have the same orientation but the orientation is not known ahead of time,
max( cat(3, x1, x2, x3, x4, x5, x6, x7, x8), [], 3)
The result would have the same orientation.
Or you could use
max( [x1(:), x2(:), x3(:), x4(:), x5(:), x6(:), x7(:), x8(:)], [], 2)
the result would be a column vector.
If you know that they are column vectors already you can leave out the (:)
  1 件のコメント
Eli Borodach
Eli Borodach 2016 年 3 月 10 日
Thank you very much!

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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by