max

4 次查看(过去 30 天)
Nicolas
Nicolas 2011-6-20
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

采纳的回答

Walter Roberson
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
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)
Nicolas
Nicolas 2011-6-20
great ! thanks a lot

请先登录,再进行评论。

更多回答(1 个)

Jan
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.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by