max
7 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Walter Roberson
on 20 Jun 2011
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 Comments
Walter Roberson
on 20 Jun 2011
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)
More Answers (1)
Jan
on 20 Jun 2011
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 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!