How can i find the maximum of three variables?

조회 수: 330 (최근 30일)
Carole
Carole 2012년 11월 27일
편집: Sarah Crimi 2018년 11월 16일
Hi, I have to find the maximum of three variables A, B, C and store the result in D. thanks
  댓글 수: 2
Sara Hafeez
Sara Hafeez 2012년 11월 30일
use max. d=max(a,b,c)
Jan
Jan 2012년 11월 30일
Have you tried this, Sara?
d = max(2,3,4)
According to the help text this fails.

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

답변 (5개)

Matt J
Matt J 2012년 11월 27일
D=max([A,B,C]);
  댓글 수: 2
Jan
Jan 2012년 11월 27일
I assume, this is a homework question.
Matt J
Matt J 2012년 11월 27일
I suppose. Can't be worth more than 1 point, though...

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


Jan
Jan 2012년 11월 27일
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.

pshymn
pshymn 2017년 4월 13일
편집: pshymn 2017년 4월 13일
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
Walter Roberson
Walter Roberson 2017년 4월 13일
That code assumes scalar values. It also assumes no nan values.

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


Image Analyst
Image Analyst 2012년 11월 27일
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
Jan
Jan 2012년 11월 28일
If A, B, C hav different sizes, [A(:), B(:), C(:)] fails. But [A(:); B(:); C(:)] with the semicolons for vertical concatenation works.

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


Sarah Crimi
Sarah Crimi 2018년 10월 5일
편집: Sarah Crimi 2018년 10월 5일
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
Stephen23
Stephen23 2018년 10월 5일
편집: Stephen23 2018년 10월 5일
"if it is vectors of different sizes you would have to do..."
Or you can just call max once (as Jan Simon showed earlier):
max([a(:);b(:);c(:)])
Sarah Crimi
Sarah Crimi 2018년 11월 16일
편집: Sarah Crimi 2018년 11월 16일
Oh yes, I see. This makes it into one vector then takes the max.

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

카테고리

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