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

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?

댓글을 달려면 로그인하십시오.

답변 (2개)

per isakson
per isakson 2015년 8월 15일
편집: per isakson 2015년 8월 15일

0 개 추천

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
Star Strider
Star Strider 2015년 8월 15일

0 개 추천

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

per isakson
per isakson 2015년 8월 15일
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. .
Star Strider
Star Strider 2015년 8월 15일
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.
Image Analyst
Image Analyst 2015년 8월 15일
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.
Image Analyst
Image Analyst 2015년 8월 15일
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)?
Star Strider
Star Strider 2015년 8월 15일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

JLK
2015년 8월 15일

댓글:

2015년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by