MATLAB: How to find the even and odd elements in a vector

I'm a student learning MATLAB and am just beginning. I have the following problem to answer:
Generate a vector of 20 random integers, each in the range from 50 to 100. Create a variable "evens" that stores all of the even numbers from the vector, and a variable "odds" that stores the odd numbers.
I created the vector of 20 random integers and named it myvec, but I have tried so many different expressions and cannot seem to find a way to find the even numbers and the odd numbers.
I've tried:
find(myvec==even)
find(myvec==odd)
find(myvec==[50:2:100])
find(myvec=[51:2:99])
None of these work, so can anyone please help?

Best Answer

hi Paul,
thats good as you created random 50,100, that first step is important,
odd and even are related to Modulus, even modulus 2 is zero :
T=round(100*rand(20,1));
evens=T(mod(T,2)==0);
odds=T(mod(T,2)~=0);
Related Question