a = 3
r = 2
n = 0:5
g=0;
for n=n
g=g+(a*(r^n))
end
% How many numbers are even?
(rem(g,2)==0)
if ans==1('Even')
end
if ans==0('Odd')
end
I want the script to be able to count how many numbers are odd and how many are even, I don't need it to tell me if the numbers are odd or even I just want the number of how many are.

댓글 수: 1

Jan
Jan 2013년 11월 15일
Do not use ans explicitly, because this is a very volatile variable.

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

답변 (2개)

Walter Roberson
Walter Roberson 2013년 11월 14일

0 개 추천

Use two counters. Initialize them to 0. As you generate it number and test it for even or odd, add one to the appropriate counter.
Kelly Kearney
Kelly Kearney 2013년 11월 14일

0 개 추천

nx = 100;
x = randi(100,nx,1); % Some example data
neven = sum(~rem(x,2));
nodd = nx - neven;

댓글 수: 3

This can be shortened by one operation ;-)
Okay then...
nodd = sum(rem(x,2));
neven = nx - nodd;
:-)
It would be interesting to compare the speed of nnz() to sum() for this case.

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

카테고리

도움말 센터File Exchange에서 Spatial Search에 대해 자세히 알아보기

질문:

2013년 11월 14일

댓글:

Jan
2013년 11월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by