Directly using Index of max value without storing
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matthew Knights
am 26 Jul. 2016
Kommentiert: Matthew Knights
am 26 Jul. 2016
Hi,
I want to use the index of a maximum value in a vector directly without storing it as a variable. i.e:
I have a vector X = [1 3 2 9 2 1], and a vector Y = [10 20 30 50 60 70] and I want to extract the value in Y corresponding to the max value in X. I know I can use:
[M,I] = max(X); then
Value = Y(I);
But I can't work out how to do this in a single line without storing the value of "I", to acheive something like:
Value = Y(...index of max value of X...)
Any help greatly appreciated.
Thanks,
Matt
2 Kommentare
Stephen23
am 26 Jul. 2016
@Matthew Knights: what output do you want if there are two identical maximum values ? E.g.:
X = [1 3 2 9 2 9]
Y = [10 20 30 50 60 70]
value = ???
Akzeptierte Antwort
Guillaume
am 26 Jul. 2016
This is one of the downside of matlab's syntax. It is not possible to chain expressions that use anything but the 1st return value. You have to use a temporary variable.
Weitere Antworten (1)
Andrei Bobrov
am 26 Jul. 2016
out = Y(max(X)==X)
4 Kommentare
Guillaume
am 26 Jul. 2016
Certainly not! There's no guarantee that the value in Y would be the same for identical max values in X.
This would work:
Y(find(max(X) == X, 1))
as long as X and Y are vectors. Reproducing the same behaviour for matrices would be harder.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!