How can I make multiple max in elegant way?

4 Ansichten (letzte 30 Tage)
Eli Borodach
Eli Borodach am 10 Mär. 2016
Kommentiert: Walter Roberson am 10 Mär. 2016
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

Akzeptierte Antwort

Ced
Ced am 10 Mär. 2016
Bearbeitet: Ced am 10 Mär. 2016
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 Kommentare
Eli Borodach
Eli Borodach am 10 Mär. 2016
Thanks you very much!
Walter Roberson
Walter Roberson am 10 Mär. 2016
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 10 Mär. 2016
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 (:)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by