Seperating evens and odds

2 visualizaciones (últimos 30 días)
-
- el 15 de Sept. de 2015
Editada: Walter Roberson el 16 de Mayo de 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 comentario
Korede Akinpelumi
Korede Akinpelumi el 16 de Mayo de 2017
Editada: Walter Roberson el 16 de Mayo de 2017
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

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 15 de Sept. de 2015
x == 2 * floor(x/2)
is true for even numbers and false for everything else

Dan Po
Dan Po el 24 de Oct. de 2016
Editada: Dan Po el 24 de Oct. de 2016
xevens=x(rem(x,2)==0)
how many ways are there to do this?

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by