Filter löschen
Filter löschen

How can I find the largest number

25 Ansichten (letzte 30 Tage)
alessandro
alessandro am 2 Jul. 2014
Hi, I have 3 numbers and I have to find the biggest one, what is the best way? This is the code:
T=200
if isempty(counter) || counter >= T
counter=0;
d1=0.1
d2=0.2
d3=0.6
else counter=counter+1
if counter>0
if d1=max([d1,d2,d3)]
...
elseif d2=max([d1,d2,d3)]
...
elseif d3=max([d1,d2,d3)]
...
end
end
end
But this code doesn't work, thank you
  3 Kommentare
alessandro
alessandro am 2 Jul. 2014
I just need to know which is the greater one in order to define another variable, for example:
if d1=max([d1,d2,d3)]
V=V1
elseif d2=max([d1,d2,d3)]
V=V1
elseif
...
Roberto Calandra
Roberto Calandra am 2 Jul. 2014
Remember that with d1=max([d1,d2,d3]) you are assigning a variable to d1but what you want is checking for equality as in d1==max([d1,d2,d3])

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Elias Gule
Elias Gule am 2 Jul. 2014
numbers = [num1 num2 num3]; % the array of numbers from which to search
maxVal = max(numbers); % the highest number

Weitere Antworten (3)

Roberto Calandra
Roberto Calandra am 2 Jul. 2014
Something like this?
switch max([d1,d2,d3])
case {d1}
V=V1
case {d2}
V=V2
case {d3}
V=V3
end

Robert Cumming
Robert Cumming am 2 Jul. 2014
you need to use
==
for comparison, i.e.
if d1==max([d1,d2,d3)]
etc...

b.lokendra sri sai
b.lokendra sri sai am 9 Jan. 2021
T=200
if isempty(counter) || counter >= T
counter=0;
d1=0.1
d2=0.2
d3=0.6
else counter=counter+1
if counter>0
if d1=max([d1,d2,d3)]
...
elseif d2=max([d1,d2,d3)]
...
elseif d3=max([d1,d2,d3)]
...
end
end
end

Kategorien

Mehr zu Dialog Boxes finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by