Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
if else - odd -even in Matlab

Subject: if else - odd -even in Matlab

From: Marie Hadji

Date: 11 Aug, 2008 22:36:02

Message: 1 of 34

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

Subject: if else - odd -even in Matlab

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Aug, 2008 23:10:27

Message: 2 of 34

In article <g7qesi$8nl$1@fred.mathworks.com>,
Marie Hadji <andry358@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

Subject: if else - odd -even in Matlab

From: bogfrog

Date: 11 Aug, 2008 23:25:29

Message: 3 of 34

if mod(x,2) == 0
  %number is even
else
  %number is odd
end

Subject: if else - odd -even in Matlab

From: Matt Fig

Date: 11 Aug, 2008 23:49:01

Message: 4 of 34

> if mod(x,2) == 0
> %number is even
> else
> %number is odd
> end


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

Subject: if else - odd -even in Matlab

From: bogfrog

Date: 12 Aug, 2008 00:09:45

Message: 5 of 34

> 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. :)

Subject: if else - odd -even in Matlab

From: us

Date: 12 Aug, 2008 02:44:02

Message: 6 of 34

"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

Subject: if else - odd -even in Matlab

From: bogfrog

Date: 12 Aug, 2008 03:33:32

Message: 7 of 34

> 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.

Subject: if else - odd -even in Matlab

From: us

Date: 12 Aug, 2008 03:56:01

Message: 8 of 34

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

Subject: if else - odd -even in Matlab

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 12 Aug, 2008 17:23:30

Message: 9 of 34

In article <g7qtdi$la3$1@fred.mathworks.com>, us <us@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

Subject: if else - odd -even in Matlab

From: Nicola Alex

Date: 12 Aug, 2008 18:49:01

Message: 10 of 34

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

Subject: if else - odd -even in Matlab

From: us

Date: 12 Aug, 2008 19:07:02

Message: 11 of 34

"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

Subject: if else - odd -even in Matlab

From: Nicola Alex

Date: 12 Aug, 2008 19:19:02

Message: 12 of 34

> 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.

Subject: if else - odd -even in Matlab

From: bogfrog

Date: 12 Aug, 2008 19:59:47

Message: 13 of 34

> 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?

Subject: if else - odd -even in Matlab

From: us

Date: 12 Aug, 2008 22:03:01

Message: 14 of 34

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

Subject: if else - odd -even in Matlab

From: us

Date: 12 Aug, 2008 22:15:04

Message: 15 of 34

"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

Subject: if else - odd -even in Matlab

From: Matt Fig

Date: 12 Aug, 2008 22:52:02

Message: 16 of 34

"us " <us@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.

Subject: if else - odd -even in Matlab

From: us

Date: 12 Aug, 2008 23:25:10

Message: 17 of 34

"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

Subject: if else - odd -even in Matlab

From: us

Date: 12 Aug, 2008 23:57:01

Message: 18 of 34

"us ":
<SNIP down to nasty typo...

> just another though

should read

  just another thought

us

Subject: if else - odd -even in Matlab

From: Matt Fig

Date: 13 Aug, 2008 02:50:03

Message: 19 of 34

"us " <us@neurol.unizh.ch> wrote in message
<g7t80d$siq$1@fred.mathworks.com>...
> "us ":
> <SNIP down to nasty typo...
>
> > just another though
>
> should read
>
> just another thought
>
> us


Point well taken, thanks us. ;)

Subject: if else - odd -even in Matlab

From: Angelina

Date: 13 Aug, 2008 03:05:40

Message: 20 of 34

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

Subject: if else - odd -even in Matlab

From: Donn Shull

Date: 13 Aug, 2008 03:25:04

Message: 21 of 34

Angelina <datacare08@gmail.com> wrote in message
<5597641.1218596771361.JavaMail.jakarta@nitrogen.mathforum.o
rg>...
> 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

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

Subject: if else - odd -even in Matlab

From: Nicola Alex

Date: 15 Aug, 2008 17:20:19

Message: 22 of 34

bogfrog <jmcgraw@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 :)

Subject: if else - odd -even in Matlab

From: Nicola Alex

Date: 15 Aug, 2008 17:30:40

Message: 23 of 34

Angelina <datacare08@gmail.com> wrote in message
<5597641.1218596771361.JavaMail.jakarta@nitrogen.mathforum.org>...
> 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.

Subject: if else - odd -even in Matlab

From: Walter Roberson

Date: 15 Aug, 2008 18:54:35

Message: 24 of 34

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...

Subject: if else - odd -even in Matlab

From: Walter Roberson

Date: 15 Aug, 2008 19:00:01

Message: 25 of 34

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?

Subject: if else - odd -even in Matlab

From: us

Date: 15 Aug, 2008 19:12:02

Message: 26 of 34

"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

Subject: if else - odd -even in Matlab

From: Matt Fig

Date: 16 Aug, 2008 06:12:03

Message: 27 of 34

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!

Subject: if else - odd -even in Matlab

From: james

Date: 10 Mar, 2011 15:05:05

Message: 28 of 34


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

Subject: if else - odd -even in Matlab

From: Matt J

Date: 10 Mar, 2011 15:25:06

Message: 29 of 34

"james " <james.franklin.uk@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

Subject: if else - odd -even in Matlab

From: james

Date: 10 Mar, 2011 15:41:05

Message: 30 of 34

oops

i meant a simple answer


add
 test=uint8(1); to the start

Subject: if else - odd -even in Matlab

From: Matt J

Date: 10 Mar, 2011 15:55:05

Message: 31 of 34

"james " <james.franklin.uk@googlemail.com> wrote in message <ilareh$ep3$1@fred.mathworks.com>...
> oops
>
> i meant a simple answer
>
>
> add
> test=uint8(1); to the start
===============


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

Subject: if else - odd -even in Matlab

From: james

Date: 10 Mar, 2011 16:58:04

Message: 32 of 34

yep you're right

Subject: if else - odd -even in Matlab

From: Steven_Lord

Date: 10 Mar, 2011 18:04:55

Message: 33 of 34



"james " <james.franklin.uk@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
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: if else - odd -even in Matlab

From: Riccardo

Date: 17 Mar, 2011 10:01:04

Message: 34 of 34

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us