MATLAB: Exercise: Add 3 to just the odd elements of x=[2 5 1 6]

array

New exercise… Add 3 to just the odd elements of x=[2 5 1 6].
I know there's some other ways to do this, probably easier, but I'd like to stick for this one for now. My line of thought is: considering that odd numbers divided by 2 are always not an integer, their remainder is always different from zero. So I used find() to look for the indexes on which there are elements that meet that condition, replaced by their values and add 3.
>> x = [2 5 1 6];
>>
>> y=find(mod(x,2)~=0)
y =
2 3
>> z=x(y)+3
=
8 4
My question is: I want the result to be the complete array, i.e., z = 2 8 4 6 (even elements included). How can I do it?
Thank you

Best Answer

Create z = x and then modify the elements of z directly using the indexes contained in y. E.g.,
z = x;
z(y) = z(y) + 3;