Seperating evens and odds

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

Korede Akinpelumi
Korede Akinpelumi 2017년 5월 16일
편집: Walter Roberson 2017년 5월 16일
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

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2015년 9월 15일

0 개 추천

x == 2 * floor(x/2)
is true for even numbers and false for everything else
Dan Po
Dan Po 2016년 10월 24일
편집: Dan Po 2016년 10월 24일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

-
-
2015년 9월 15일

편집:

2017년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by