MATLAB Answers

0

Seperating evens and odds

Asked by Ahmet Batin Deliorman on 15 Sep 2015
Latest activity Edited by Walter Roberson
on 16 May 2017
What would be the method to solve the problem below
Generate a vector of 20 random integers, each in the range of 50 to 100 and store it in vec. Create a variable evens that stores all of the even numbers from the vector, and a variable odds that stores all of the odd numbers.

  1 Comments

function [evens,odds]=assignm
A=randi(51,1,20)+49;
B=50:2:100;
C=51:2:99;
evens=intersect(A,B);
odds=intersect(A,C);
end

Sign in to comment.

Tags

2 Answers

Answer by Walter Roberson
on 15 Sep 2015

x == 2 * floor(x/2)
is true for even numbers and false for everything else

  0 Comments

Sign in to comment.


Answer by Dan Po
on 24 Oct 2016
Edited by Dan Po
on 24 Oct 2016

xevens=x(rem(x,2)==0)
how many ways are there to do this?

  1 Comments

Walter Roberson
on 24 Oct 2016
xevens = x(~rem(x,2));

Sign in to comment.



Translated by