Add 3 to the values of x that are even

5 views (last 30 days)
x=[7 6 1 2 0 -1 4 3 -2 0]
how do I do this in smart short way?

Accepted Answer

Friedrich
Friedrich on 11 Oct 2011
Hi,
x(find(mod(x,2) == 0)) = x(find(mod(x,2) == 0)) + 3
or
ind = find(mod(x,2) == 0);
x(ind) = x(ind) + 3;

More Answers (2)

Arash
Arash on 11 Oct 2011
Thank you. It works !!

Walter Roberson
Walter Roberson on 11 Oct 2011
x = x + 3 * ~mod(x,2);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!