How can i find the maximum of three variables?
Mostra commenti meno recenti
Hi, I have to find the maximum of three variables A, B, C and store the result in D. thanks
2 Commenti
Sara Hafeez
il 30 Nov 2012
use max. d=max(a,b,c)
Jan
il 30 Nov 2012
Have you tried this, Sara?
d = max(2,3,4)
According to the help text this fails.
Risposte (5)
Jan
il 27 Nov 2012
How would you find the maximum of two variables?
docserach maximum
Then if you have the maximum of two variables, you can compare it to the third one.
maybe, i am a little late, but i experienced the same problem. here is my solution. it works, tried. function which finds maximum of three variable.
function [p] = MAX(g,h,j)
if g>h && g>j
p=g;
elseif h>g && h>j
p=h;
else
p=j;
end
1 Commento
Walter Roberson
il 13 Apr 2017
That code assumes scalar values. It also assumes no nan values.
Image Analyst
il 27 Nov 2012
In general, for A, B, and C being numerical arrays of any size (possibly different than each other):
% Define 3 double matrixes of different sizes
A = rand(3);
B = rand(4);
C = rand(5);
% Now get the max overall
D = max([A(:), B(:), C(:)]) % Note use of (:) to turn arrays into vectors.
If they're cell arrays or structures though, let us know because that would be different.
1 Commento
Jan
il 28 Nov 2012
If A, B, C hav different sizes, [A(:), B(:), C(:)] fails. But [A(:); B(:); C(:)] with the semicolons for vertical concatenation works.
Sarah Crimi
il 5 Ott 2018
Modificato: Sarah Crimi
il 5 Ott 2018
0 voti
if it is vectors of different sizes, you would have to do max(max(a),max(b)),max(max(b),max(c)). So, it takes max of the first and second vectors and compares the values, and max of the second and third vectors, then takes the max of those two numbers.
2 Commenti
"if it is vectors of different sizes you would have to do..."
max([a(:);b(:);c(:)])
Sarah Crimi
il 16 Nov 2018
Modificato: Sarah Crimi
il 16 Nov 2018
Oh yes, I see. This makes it into one vector then takes the max.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!