change numbers in array

2 visualizaciones (últimos 30 días)
Nathan Formby
Nathan Formby el 14 de Nov. de 2019
Comentada: JESUS DAVID ARIZA ROYETH el 15 de Nov. de 2019
Say that an input ask you to input numbers
Ex: [50 -6 14]
, but it won't allow you to enter numbers equal to or less than zero.
How do I build an if statement that would allow me to replace the negative or zero number?
Reminder: all the inputs are placed all in one input( at the same time); not seperately.

Respuesta aceptada

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH el 14 de Nov. de 2019
Editada: JESUS DAVID ARIZA ROYETH el 15 de Nov. de 2019
you can do this:
array = input('Please enter array');
array(array<=0)=nan
or this :
a=true;
while a
array = input('Please enter array with numbers >= 0');
a=sum(array<0)>0;
end
edit:
a=false;
while not(a)
array = input('Please enter array 1x3:');
a=isequal(size(array),[1,3]);
end
for k=1:3
if array(k)<=0
a=true;
while a
newvalue=input(num2str(k,'Please replace index %i, because must be greater than 0'));
a=newvalue<=0;
end
array(k)=newvalue;
end
end
disp(array);
  2 comentarios
Nathan Formby
Nathan Formby el 15 de Nov. de 2019
To be more specific. The task is to enter 1 array with 1 row and 3 columns. If one of the numbers is equal to or less than zero it must be replaced.
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH el 15 de Nov. de 2019
here:
a=false;
while not(a)
array = input('Please enter array 1x3:');
a=isequal(size(array),[1,3]);
end
for k=1:3
if array(k)<=0
a=true;
while a
newvalue=input(num2str(k,'Please replace index %i, because must be greater than 0'));
a=newvalue<=0;
end
array(k)=newvalue;
end
end
disp(array);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by