Creating vector only containing odd values

A have a script which takes in x and y values i.e.
x = [1 2 3 4 5];
y = x.^2;
and I require the script to recreate the vectors so that both the vectors only contain the odd values and the even values round down to the nearest odd value i.e.
x = [1 1 3 3 5];
How can this be done without using loops?

1 Comment

It looks like you got some solutions but only for x, not for y, and that's because you're unclear on what to do with y.
Does that requirement apply to the original y that used the original x, or do you recompute y using the altered x?
x = [1 2 3 4 5]
y1 = x.^2
x = [1,1,3,3,5]
y2 = x.^2
x =
1 2 3 4 5
y =
1 4 9 16 25
So the new y would be
y1 =
1 3 9 15 25
Or using the new altered x:
x =
1 1 3 3 5
y2 =
1 1 9 9 25
So do you want y to be y1 or y2 in my example? Which is it?
This sounds a lot like homework. Is it?

Sign in to comment.

Answers (2)

per isakson
per isakson on 15 Aug 2015
Edited: per isakson on 15 Aug 2015
Try
>> x = [1 2 3 4 5];
>> x(rem(x,2)==0) = x(rem(x,2)==0)-1
x =
1 1 3 3 5
or rather
>> x = [1 2 3 4 5];
>> is_even = rem(x,2)==0;
>> x(is_even) = x(is_even)-1
x =
1 1 3 3 5
Interesting problem. I couldn’t resist the challenge!
x = [1 2 3 4 5];
xo = 2*ceil(x/2)-1
xo =
1 1 3 3 5
It seems to be robust to longer vectors.

6 Comments

This is faster, but in what way would it be more robust?
There is no error checking in either. One should assert that x only contains whole numbers - I guess based on the example that that should be the case. .
I mentioned it as ‘robust to longer vectors’ meaning that it works with positive (and by definition, non-zero) integer vectors longer than this one. The Question illustrates a positive integer vector, so I assume those are the vectors it deals with, especially since ‘even’ and ‘odd’ also refer only to integers.
To work with negative integer vectors, we would need to know what JLK would want the result to be.
He asked about y also: "...so that both the vectors...". What is your interpretation of how to deal with y (see my question above)?
My impression is that ‘y’ is calcualted from ‘x’, since JLK doesn’t give an example of what ‘y’ should be. However, it seems to me (offered without formal proof) that the square of an odd number would be an odd number, if that is the issue.
Consider:
x = 1:2:25;
y = x.^2;
y_chk = rem(y,2);
as an ersatz numerical proof.
Yes, but which x? Do you fix both the original x and y (in which case the fixed y is based on the original x), or do you recompute y using the new x (so you can throw away the original y since it was never needed in the first place)?
I interpreted ‘...both vectors...’ as referring to the derived all-odd version of ‘x’, and the ‘y’ calculated from it.
Until JLK clarifies these issues, I’ll stop speculating. There could be boundless interpretations.

Sign in to comment.

Asked:

JLK
on 15 Aug 2015

Commented:

on 15 Aug 2015

Community Treasure Hunt

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

Start Hunting!