o=[ 2 5 8 23 67 9 66 21],i need to form a vector-column p and r so that p has odd numers from o=[..] and r that has even numbers from o=[..]

1 view (last 30 days)
o=[ 2 5 8 23 67 9 66 21],i need to form a vector-column p and r so that p has odd numers from o=[..] and r that has even numbers from o=[..]

Answers (1)

Star Strider
Star Strider on 23 Apr 2015
Use the rem (remainder after division) function to define your vector elements. Dividing an even integer by 2 results in a zero remainder, and dividing an odd integer by 2 results in a remainder of 1.
I’ll let you figure out how ‘logical indexing’ works:
o=[ 2 5 8 23 67 9 66 21];
P = o(rem(o,2) ~= 0); % Odd
r = o(rem(o,2) == 0); % Even
  3 Comments
Star Strider
Star Strider on 23 Apr 2015
The constraint of not using rem simply means that you have to create your own version. Divide all elements by 2, then use fix or one of its friends to see if there are any remainders:
o=[ 2 5 8 23 67 9 66 21];
q = fix(o/2)-o/2;
p = o(q < 0)
r = o(q == 0)

Sign in to comment.

Categories

Find more on Elementary Math 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!