Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

if else - odd -even in Matlab

2,548 views
Skip to first unread message

Marie Hadji

unread,
Aug 11, 2008, 6:36:02 PM8/11/08
to
Hello everybody!

Can anybody guide me how to do this right in matlab.
Actually How I can distinguish odd from even from numbers (0
to 8 with if and else)?

ex. if x==1 or x==3 or x==5 or x==7 do that else if x==0 or
x==2 or x==4 or x==6 or x==8 do that.

if (x==1 or x==3 or x==5 or x==7)
.
.%do this
.
elseif (x==0 or x==2 or x==4 or x==6 or x==8)
.
.%do this
.
end;

Kindly suggest me how to do it..

Thanks in advance

Walter Roberson

unread,
Aug 11, 2008, 7:10:27 PM8/11/08
to
In article <g7qesi$8nl$1...@fred.mathworks.com>,
Marie Hadji <andr...@hotmail.com> wrote:

>Can anybody guide me how to do this right in matlab.
>Actually How I can distinguish odd from even from numbers (0
>to 8 with if and else)?

>ex. if x==1 or x==3 or x==5 or x==7 do that else if x==0 or
>x==2 or x==4 or x==6 or x==8 do that.

first = @(v) v(1);
if first(factor(x)) - 2
%odd
else
%even
end
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter

bogfrog

unread,
Aug 11, 2008, 7:25:29 PM8/11/08
to
if mod(x,2) == 0
%number is even
else
%number is odd
end

Matt Fig

unread,
Aug 11, 2008, 7:49:01 PM8/11/08
to


The explicit comparison is not necessary since mod(x,2) is
zero or one for integer x:


if mod(x,2)
disp('odd')
else
disp('even')
end

bogfrog

unread,
Aug 11, 2008, 8:09:45 PM8/11/08
to
> The explicit comparison is not necessary since
> mod(x,2) is
> zero or one for integer x:

True, but I thought I'd do it that way to emphasize what's going on, so he/she would understand it better. :)

us

unread,
Aug 11, 2008, 10:44:02 PM8/11/08
to
"Marie Hadji":
<SNIP odd evergreen...

> Actually How I can distinguish

> odd from even from numbers...

one of the many solutions

x=-4:4;
tf=bitget(abs(x),1)~=0;
odd=x(tf)
% odd = -3 -1 1 3
even=x(~tf)
% even = -4 -2 0 2 4

note: do NOT use rem/mod-stuff! it comes with unnecessary
FP-issues, which have been shown several times here at
CSSM...

us

bogfrog

unread,
Aug 11, 2008, 11:33:32 PM8/11/08
to
> note: do NOT use rem/mod-stuff! it comes with
> unnecessary
> FP-issues, which have been shown several times here
> at
> CSSM...


What does this mean? Can you refer me to a post? (I searched for "rem mod fp" and nothing shows up.)

Thanks.

us

unread,
Aug 11, 2008, 11:56:01 PM8/11/08
to
bogfrog:
<SNIP looking for an old even/bit... post...

> Can you refer me to a post

one of the latest can be found here

% on
http://groups.google.com/group/comp.soft-sys.matlab/topics

% search for
even numbers bitand rem

% look for instance at the one posted on aug 24, 2007

us

Walter Roberson

unread,
Aug 12, 2008, 1:23:30 PM8/12/08
to
In article <g7qtdi$la3$1...@fred.mathworks.com>, us <u...@neurol.unizh.ch> wrote:
>"Marie Hadji":

> tf=bitget(abs(x),1)~=0;

Interesting, bitget() is not documented to work with doubles
(at least as of R2007a) but it does, for bits 1 to 52.

I'll file a documentation request. (Now filed.)
--
"The quirks and arbitrariness we observe force us to the
conclusion that ours is not the only universe." -- Walter Kistler

Nicola Alex

unread,
Aug 12, 2008, 2:49:01 PM8/12/08
to
Hi

> Actually How I can distinguish odd from even from numbers (0
> to 8 with if and else)?

I've always used
if floor(number)==number, %then even
else % odd
end

us

unread,
Aug 12, 2008, 3:07:02 PM8/12/08
to
"Nicola Alex ":
<SNIP one lucky person who never had an odd day...

> I've always used
> if floor(number)==number, %then even
> else % odd
> end

now - i can better appreciate the results of your last
science paper...

x=1:5;
floor(x)==x
% ans = 1 1 1 1 1

just a thought...
us

Nicola Alex

unread,
Aug 12, 2008, 3:19:02 PM8/12/08
to
> now - i can better appreciate the results of your last
> science paper...

???

> x=1:5;
> floor(x)==x
> % ans = 1 1 1 1 1

I guess that's what I call 'vector', which usually is a
little different from 'number'.
The topic quest was


> How I can distinguish odd from even from numbers (0
to 8 with if and else)?

If I have a vector, I'd just use the
floor(vector(i))==vector(i) inside a for cycle.

bogfrog

unread,
Aug 12, 2008, 3:59:47 PM8/12/08
to
> I've always used
> if floor(number)==number, %then even
> else % odd
> end


What? How can that possibly work?

floor(5)==5

is true. So is 5 even?

As for not using mod()... I still don't really see the big deal. Why would you test for even/odd on non-integers anyway?

us

unread,
Aug 12, 2008, 6:03:01 PM8/12/08
to
bogfrog:
<SNIP still clueless...

> As for not using mod()... I still don't really see the

> big deal....
> Why would you test for even/odd on non-integers anyway...

well, think more, think computer...

x=1:5;
mod(x,2)
% ans = 1 0 1 0 1
% fine, now
x=realmax+(-5:0);
mod(x,2)
% ans = 0 0 0 0 0 0 % allow me to put an ! here
x=x/1e100;
mod(x,2)
% ans = 0 0 0 0 0 0 % ... and again
% because of the uncertainty
eps(realmax)
% ans = 1.9958e+292
% whereas
bitand(x,1) % -or- bitget(...)
%
% will show you right away that you will not
% get a real answer...
%{
??? Error using ==> bitand
Exceeded value of bitmax.
%}

better believe me, this naive mod/rem approach has been a
bigger issue for many a people than you might like... (not
everybody's even/odd problem is in the range of [-10/10])

us

us

unread,
Aug 12, 2008, 6:15:04 PM8/12/08
to
"Nicola Alex ":
<SNIP needs to look into some beginner's guide(s)...

> I guess that's what I call 'vector', which usually is a
> little different from 'number'.
> The topic quest was
> > How I can distinguish odd from even from numbers (0
> to 8 with if and else)?

> If I have a vector, I'd just use the

> floor(vector(i))==vector(i) inside a for cycle...

1) all of this does not make sense...
2) if i'd ever use your number-approach, i'd end up with
this nonsense

% create a bunch of even/odd numbers
vector=0:5;
for i=1:numel(vector)
r=floor(vector(i))==vector(i); % your snipplet...
disp(sprintf('v(%1d) = %2d: %2d',i,vector(i),r));
end
%{
v(1) = 0: 1 % your even
v(2) = 1: 1 % your even
v(3) = 2: 1 % your even
v(4) = 3: 1 % ...
v(5) = 4: 1
v(6) = 5: 1
%}

us

Matt Fig

unread,
Aug 12, 2008, 6:52:02 PM8/12/08
to
"us " <u...@neurol.unizh.ch> wrote in message
> snippity-snipperoo

> x=1:5;
> mod(x,2)
> % ans = 1 0 1 0 1
> % fine, now
> x=realmax+(-5:0);
> mod(x,2)
> % ans = 0 0 0 0 0 0 % allow me to put an ! here
> x=x/1e100;
> mod(x,2)
> % ans = 0 0 0 0 0 0 % ... and again
> us

us, you make some good points, but is realmax an integer?
Please forgive me for my lack of years of Matlabing (I am
still a learner!), but what is wrong with this:

x = double(intmax)+(-10:10);
mod(x,2)' % Alternating 1 0 1....
x2 = round(realmax) + (-10:10);
mod(x,2)' % Alternating 1 0 1....

Check this conclusion please:
So as long as we sure we are using integers, the mod method
works.

us

unread,
Aug 12, 2008, 7:25:10 PM8/12/08
to
"Matt Fig":
<SNIP makging the usual good point...

> ...but is realmax an integer...


> x = double(intmax)+(-10:10);
> mod(x,2)' % Alternating 1 0 1....
> x2 = round(realmax) + (-10:10);
> mod(x,2)' % Alternating 1 0 1....
> Check this conclusion please:
> So as long as we sure we are using integers, the mod

> method works...

hi matt, that is JUST the point!

realmax is: the largest pos FP number
most CSSMers are living in this realm of bits and bytes
intmax is: the largest pos INT number
most CSSMers don't care about the real world

now, as long as people CHECK for the validity of their
numbers, ie, abs(number) <= intmax, NOTHING would be wrong
with the unholy MOD/REM critters...
however, people tend NOT to do so, and tend to have
(apparent) false ML results, and tend to accuse of ML being
buggy, and (then) tend to whine about this on CSSM...

all i like to get across is this pedestrian message:
- unless you cognitively check that your number can truly
be represented by a unique bit-pattern (ie, int), you
should NOT naively use REM/MOD for your calculations...
- needless to say that any suchy CHECK requires additional
code in your snippet, which makes ML life (even?) more
cumbersome...
- needless to say that most people (at first) do NOT care
about this FP issue...
- needless to say that the BIT... family of stock functions
takes care of this - by a nasty error message...

just another though
as ever, best
urs

us

unread,
Aug 12, 2008, 7:57:01 PM8/12/08
to
"us ":
<SNIP down to nasty typo...

> just another though

should read

just another thought

us

Matt Fig

unread,
Aug 12, 2008, 10:50:03 PM8/12/08
to
"us " <u...@neurol.unizh.ch> wrote in message
<g7t80d$siq$1...@fred.mathworks.com>...


Point well taken, thanks us. ;)

Angelina

unread,
Aug 12, 2008, 11:05:40 PM8/12/08
to
Well, it's a very typical job to identify the difference between the even & odd. Most of the times,I do get confused.I hope if I could get an easy method for this task.

Angelina

http://www.treatmentcenters.org/washington

Donn Shull

unread,
Aug 12, 2008, 11:25:04 PM8/12/08
to
Angelina <datac...@gmail.com> wrote in message
<5597641.12185967713...@nitrogen.mathforum.o
rg>...

Hi Marie,

I have one other method to solve your problem. It does not
use if else end but I think it is very readable.

switch x
case{0,2,4,6,8}
str = 'Even Set';
case{1,3,5,7}
str = 'Odd Set';
otherwise
str = 'Otherwise';
end

Good Luck,

Donn


Nicola Alex

unread,
Aug 15, 2008, 1:20:19 PM8/15/08
to
bogfrog <jmc...@rcn.com> wrote in message
> What? How can that possibly work?
>
> floor(5)==5
>
> is true. So is 5 even?

:D

You're absolutely right; I forgot a piece:

if floor(number/2) == number/2, % then it's even
else % it's odd


Sorry guys :)

Nicola Alex

unread,
Aug 15, 2008, 1:30:40 PM8/15/08
to
> Well, it's a very typical job to identify the difference
between the even & odd. Most of the times,I do get
confused.I hope if I could get an easy method for this task.

???

By definition every even number is divisible by 2.
Checking this is sufficient to identify even numbers.
Where's the problem?

It's so easy I actually forgot the most stupid part, when I
answered this topic: dividing the number by 2 prior to
verify it.

Walter Roberson

unread,
Aug 15, 2008, 2:54:35 PM8/15/08
to
Nicola Alex wrote:

> You're absolutely right; I forgot a piece:

> if floor(number/2) == number/2, % then it's even
> else % it's odd

>> floor(inf/2) == inf/2

ans =

1

Guess inf must be even then...

>> floor((inf+1)/2) == (inf+1)/2

ans =

1

And inf+1 is even too...

Walter Roberson

unread,
Aug 15, 2008, 3:00:01 PM8/15/08
to
Nicola Alex wrote:

> By definition every even number is divisible by 2.
> Checking this is sufficient to identify even numbers.
> Where's the problem?

>> floor(i/2) == i/2

ans =

0

>> floor((i+1)/2) == (i+1)/2

ans =

0


So is i even or odd?

us

unread,
Aug 15, 2008, 3:12:02 PM8/15/08
to
"Nicola Alex ":
<SNIP continuing squabble...

> You're absolutely right; I forgot a piece:
> if floor(number/2) == number/2, % then it's even
> else % it's odd
> Sorry guys :)

%{
BITGET
BITAND
%}

n=realmax+(-5:0); % a possible number a user may have
floor(n/2)==n/2
% ans = 1 1 1 1 1 1

us

Matt Fig

unread,
Aug 16, 2008, 2:12:03 AM8/16/08
to
A little experiment:

% A starting point to naively look for the
% maximum value for which rem/mod works.
num = bitmax - 10000;

tf1 = rem(num,2);
tf2 = rem(num+1,2);

while tf1 ~= tf2
num = num+1; % Find that max value...
tf1 = rem(num,2);
tf2 = rem(num+1,2);
end

num
epsnum = eps(num) % This is interesting.
epsltnum = eps(num-1) % Ah-ha
% As us was saying, it is no coincidence that we get:
num2str(bitget(num-1,1:52)) % Here we are maxed out.
try
bitget(num,1:52) % Here we get an error
catch
disp(lasterr)
end
% and also
num - bitmax
% All numbers > bitmax are even (with rem/mod)!!!
% Lesson: at least with the bitget test,
% we get an error, better than false output!

james

unread,
Mar 10, 2011, 10:05:05 AM3/10/11
to

a dead thread without an answer

x = uint8(1);

x= a number of your choice

test=x/2;

decide=isinteger(test);

if decide=1 (then x was a even number)
do something
end

if decide=0 (then x was an odd number)
do something
end

Matt J

unread,
Mar 10, 2011, 10:25:06 AM3/10/11
to
"james " <james.fr...@googlemail.com> wrote in message <ilapb1$nic$1...@fred.mathworks.com>...

>
> a dead thread without an answer
>
> x = uint8(1);
>
> x= a number of your choice
>
> test=x/2;
>
> decide=isinteger(test);
===================


I'm not sure why you think the many previous posts provided no answer. Regardless, your proposal doesn't work, as this example shows

>> x=uint8(1);
>> x=4;
>> test=x/2

test =

2

>> decide=isinteger(test) %concludes that the choice x=4 was odd

decide =

0


A modification of your idea that would work is as follows

>> x=4; decide=isequal(x/2,uint64(x/2)),

decide =

1

>> x=5; decide=isequal(x/2,uint64(x/2)),

decide =

0

james

unread,
Mar 10, 2011, 10:41:05 AM3/10/11
to
oops

i meant a simple answer


add
test=uint8(1); to the start

Matt J

unread,
Mar 10, 2011, 10:55:05 AM3/10/11
to
"james " <james.fr...@googlemail.com> wrote in message <ilareh$ep3$1...@fred.mathworks.com>...
===============


Nope. It still doesn't work. Nor does it seem simpler than previous proposals, as it requires at least 5 lines of code

>> test=uint8(1); x=uint8(1); x=4; test=x/2;
>> decide=isinteger(test)


decide =

0

james

unread,
Mar 10, 2011, 11:58:04 AM3/10/11
to
yep you're right

Steven_Lord

unread,
Mar 10, 2011, 1:04:55 PM3/10/11
to

"james " <james.fr...@googlemail.com> wrote in message

news:ilapb1$nic$1...@fred.mathworks.com...


>
> a dead thread without an answer
>
> x = uint8(1);
>
> x= a number of your choice
>
> test=x/2;
>
> decide=isinteger(test);

ISINTEGER probably doesn't do what you think it does.

http://www.mathworks.com/help/techdoc/ref/isinteger.html

It determines if the input is of an integer _data type_, not if the value is
an integer _value_.

isinteger(double(5)) % false since double is not an integer data type

Take a look at:

x = uint8(5);
test = x/2;
isinteger(test) % true because the result of dividing a uint8 by a double
scalar is of class uint8

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Riccardo

unread,
Mar 17, 2011, 6:01:04 AM3/17/11
to
I have only quickly scrolled through the numerous answers so forgive me if this has already been suggested.

I always like to go for code reusability and readability, so if I were to do it I would create a function as follows:

function result = isEven(this)
result = mod(this,2);

Then my code would look like:

if isEven(theValue)
% --- do something
else
% --- do something else
end


I hope it helps

Riccardo

0 new messages