How to count how many numbers are odd or even?
2 views (last 30 days)
Show older comments
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 Comment
Answers (2)
Walter Roberson
on 14 Nov 2013
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.
0 Comments
Kelly Kearney
on 14 Nov 2013
nx = 100;
x = randi(100,nx,1); % Some example data
neven = sum(~rem(x,2));
nodd = nx - neven;
3 Comments
Walter Roberson
on 15 Nov 2013
It would be interesting to compare the speed of nnz() to sum() for this case.
See Also
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!