Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Even/odd
Date: Thu, 23 Aug 2007 15:21:31 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 28
Message-ID: <fak8lr$34$1@fred.mathworks.com>
References: <fak6ev$t2k$1@fred.mathworks.com> <fak7e5$d6q$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1187882491 100 172.30.248.37 (23 Aug 2007 15:21:31 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 23 Aug 2007 15:21:31 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:425231



Steve Amphlett:
<SNIP down to typical even/odd algorithm...

> mod(x,2)
> 0 if even, 1 if odd

% now, since even/odd should be integers,
% this may fail, eg,
     n=-4:4;
     m=n;
     even=mod(m,2)==0;
     odd=~even;
     [m;even;odd]
% if the user is not careful
     m=pi*n;
     even=mod(m,2)==0;
     odd=~even;
     [m;even;odd]
% a better way may be this
     odd=bitand(abs(n),1);
     even=~odd;
     [n;even;odd]
% ...which would give an error early on
     odd=bitand(abs(m),1);

just a thought
us