MATLAB Answers

Seperating evens and odds

Ahmet Batin Deliorman 님이 질문을 제출함. 15 Sep 2015
최근 활동 Walter Roberson 님이 편집함. 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

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

로그인 to comment.

태그

답변 수: 2

Walter Roberson 님의 답변 15 Sep 2015

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

  댓글 수: 0

로그인 to comment.


Dan Po 님의 답변 24 Oct 2016
Dan Po 님이 편집함. 24 Oct 2016

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

  댓글 수: 1

xevens = x(~rem(x,2));

로그인 to comment.



Translated by