max
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi,
I'm selecting the max of three different values
distmax=max([dist1 dist2 dist3])
which gives for example
distmax= 3.434
is there a possibility to have the name of the value instead of the value itself, like
distmax = dist1
thank you
댓글 수: 0
채택된 답변
Walter Roberson
2011년 6월 20일
No.
You can, however, use
[distmax, idx] = max([dist1 dist2 dist3])
and then idx will indicate which index in to the [dist1 dist2 dist3] matrix was the maximum. To figure out which of the variables that came from, you would need to know the size()'s of the variables.
It would be easier if you used
[distmax, idx] = max([max(dist1(:)), max(dist2(:)), max(dist3(:))])
and then idx would indicate which variable number the max was found in; relating the variable number to the variable name is a different matter.
Plain
[distmax, idx] = max([dist1 dist2 dist3])
is the abbreviation for the above for the special case where dist1, dist2, and dist3 are all guaranteed to be scalars (something your question does not allow us to assume).
Question: what do you want to happen if the identical maximum value exists in more than one of the variables?
댓글 수: 3
Walter Roberson
2011년 6월 20일
v = [j i;k i;k j];
[distmax, idx] = max([distij distik distjk]);
xextr = x(v(idx,:));
yextr = y(v(idx,:));
First extreme would be xextr(1), yextr(1), second would be xextr(2), yextr(2)
추가 답변 (1개)
Jan
2011년 6월 20일
It is possible to get the name:
a = rand;
b = rand;
c = rand;
function Name = GetMaxName(a, b, c)
[Value, Index] = max([a, b, c]);
Name = inputname(Index);
But Wlater's solution is much more stable and reliable, because INPUTNAME replies an empty string for temporary objects, e.g. in "GetMaxName(round(a) ...)". Using the name of a variable as information is a fragil programming method.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!