필터 지우기
필터 지우기

Directly using Index of max value without storing

조회 수: 3 (최근 30일)
Matthew Knights
Matthew Knights 2016년 7월 26일
댓글: Matthew Knights 2016년 7월 26일
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
Stephen23
Stephen23 2016년 7월 26일
@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 = ???
Matthew Knights
Matthew Knights 2016년 7월 26일
Good question.
My problem is such that this won't occur for my application, so it is something I hadn't considered.
But for argument's sake I guess in an ideal world you would want both Y values given... But I see how this makes things more complicated.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 7월 26일
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.
  댓글 수: 1
Matthew Knights
Matthew Knights 2016년 7월 26일
Thanks Guillaume.
I guess that's a fair sacrifice for Matlab's positive qualities.
It's always reassuring to find that the question you ask has a genuine reason behid it rather than just missing something glaringly obvious.
Many thanks,
Matthew

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2016년 7월 26일
out = Y(max(X)==X)
  댓글 수: 4
Guillaume
Guillaume 2016년 7월 26일
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.
Matthew Knights
Matthew Knights 2016년 7월 26일
Thank you for the suggestions. My application is fairly simple so these will be applicable regardless of limitations for more complex problems. But I'm always glad to understand the whole scope for future use.
Many thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by