I need help writing a program to pick the largest number in a list
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
justin simmons
el 12 de Feb. de 2017
Comentada: Image Analyst
el 13 de Feb. de 2017
I was given:
clc;
n=input('how many numbers are in your list?');
for i=1:n
y=['enter a number', num2str(i)];
disp (y)
x(i)=input('');
end
and the list of numbers to plug in. I need to finish the program so it will choose the largest from my list. This is what I have so far:
if x(i)<=y
[newx,newy]=swap (x,y)
newx=y
newy=x
else x(i)>y
disp('The largest number in the list is')
end
This program runs but does not give me a single value. What should i do to fix it?
12 comentarios
Walter Roberson
el 13 de Feb. de 2017
Computers cannot "just look", but they can use the same kind of procedure you describe.
Respuesta aceptada
Seyedali Mirjalili
el 12 de Feb. de 2017
Editada: Seyedali Mirjalili
el 12 de Feb. de 2017
Try this:
my_max = -inf;
for k=1:length(y) % y is your array
if my_max < y(k)
my_max = y(k);
end
end
disp(my_max)
This is computationally expensive though. You can find the maximum easily with the function max:
my_max = max(y'); %y is your array. If it is already a column vector, you do not need to transpose it
2 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!