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

1 次查看(过去 30 天)
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.

采纳的回答

michael
michael 2016-10-3
vec=vec+1+mod(vec,2)*2;

更多回答(1 个)

Walter Roberson
Walter Roberson 2016-10-3
Hint:
mask1 = vec > 7
vec(mask1) = vec(mask1) * 5

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by