How do I add 1 to all even numbers and add 3 to all odd numbers in a vector?

1 visualización (últimos 30 días)
I have no idea how to do this.I already created a vector by typing: vec = round(-10 + 35* rand(1,30)); and next I am suppose to add 1 to all the even numbers and add 3 to all odd numbers. Thank you in advance for the help.

Respuesta aceptada

michael
michael el 3 de Oct. de 2016
vec=vec+1+mod(vec,2)*2;

Más respuestas (1)

Walter Roberson
Walter Roberson el 3 de Oct. de 2016
Hint:
mask1 = vec > 7
vec(mask1) = vec(mask1) * 5

Categorías

Más información sobre MATLAB 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