Creating vector only containing odd values
Ältere Kommentare anzeigen
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 Kommentar
Image Analyst
am 15 Aug. 2015
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?
Antworten (2)
per isakson
am 15 Aug. 2015
Bearbeitet: per isakson
am 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
Star Strider
am 15 Aug. 2015
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 Kommentare
per isakson
am 15 Aug. 2015
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
am 15 Aug. 2015
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
am 15 Aug. 2015
He asked about y also: "...so that both the vectors...". What is your interpretation of how to deal with y (see my question above)?
Star Strider
am 15 Aug. 2015
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
am 15 Aug. 2015
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
am 15 Aug. 2015
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.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!