if even/odd statement

21 visualizaciones (últimos 30 días)
John
John el 30 de Mzo. de 2011
Hi, I have to write a code that doubles all odd numbers, and square all even numbers from 1 to 100. Can some1 please help me out.

Respuesta aceptada

Matt Fig
Matt Fig el 30 de Mzo. de 2011
Where are you stuck? What have you tried?
.
EDIT In response to your comment...
IF statements don't select out portions of an array like that. You will need an index to do that. Here are some clues, copy and paste into MATLAB:
x = 1:10
idx = mod(x,2)==1 % This is a logical index.
x(idx)
x(~idx)
  2 comentarios
John
John el 30 de Mzo. de 2011
i've try this so far but its not working..
x=1:1:100;
if a=2*x+1 %odds
a*2
else b=2*x %evens
b^2
end
John
John el 30 de Mzo. de 2011
I've got it. Thank you very much!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown 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