How to find max value of three varibles using if,else,end.

10 vues (au cours des 30 derniers jours)
Giuseppe
Giuseppe le 12 Mar 2014
The variables are:
a=20
b=10
c=30
How do i find the max without using built in functions.
I believe it is
if a>b && a>c
disp(a)
elseif b>a && b>c
disp(b)
else
disp(c)
end

Réponse acceptée

Chris C
Chris C le 12 Mar 2014
Modifié(e) : Chris C le 12 Mar 2014
Give this code a shot. You would be able to make the variable "data" listed here as a vector of whatever data you like and this should still work. If you had a matrix instead of a vector, however, you would need to loop around both the rows and columns.
a = 20;
b = 10;
c = 30;
data = [a,b,c];
largest = data(1);
for i = 1:length(data)
if data(i) > largest;
largest = data(i);
end
end
  2 commentaires
Giuseppe
Giuseppe le 14 Mar 2014
thank you very much
Ram Charan
Ram Charan le 12 Fév 2021
thank you so much

Connectez-vous pour commenter.

Plus de réponses (2)

siva naga sai babu
siva naga sai babu le 17 Fév 2021
a = 20;
b = 10;
c = 30;
data = [a,b,c];
largest = data(1);
for i = 1:length(data)
if data(i) > largest;
largest = data(i);
end
end

Vallambotla
Vallambotla le 28 Nov 2022
a = 20;
b = 10;
c = 30;
data = [a,b,c];
largest = data(1);
for i = 1:length(data)
if data(i) > largest;
largest = data(i);
end

Catégories

En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by