<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155034</link>
    <title>MATLAB Central Newsreader - Even/odd</title>
    <description>Feed for thread: Even/odd</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 23 Aug 2007 14:43:43 -0400</pubDate>
      <title>Even/odd</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155034#389012</link>
      <author>Edward Seger</author>
      <description>How about even and odd functions built-in:&lt;br&gt;
&lt;br&gt;
bool x;&lt;br&gt;
&lt;br&gt;
x = even(num);&lt;br&gt;
x = odd(num);</description>
    </item>
    <item>
      <pubDate>Thu, 23 Aug 2007 15:00:21 -0400</pubDate>
      <title>Re: Even/odd</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155034#389020</link>
      <author>Steve Amphlett</author>
      <description>&quot;Edward Seger&quot; &amp;lt;edward.h.seger@saic.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fak6ev$t2k$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; How about even and odd functions built-in:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; bool x;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x = even(num);&lt;br&gt;
&amp;gt; x = odd(num);&lt;br&gt;
&lt;br&gt;
So what's wrong with &amp;lt;mod&amp;gt; ?&lt;br&gt;
&lt;br&gt;
mod(x,2)&lt;br&gt;
&lt;br&gt;
0 if even, 1 if odd</description>
    </item>
    <item>
      <pubDate>Thu, 23 Aug 2007 15:21:31 -0400</pubDate>
      <title>Re: Even/odd</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155034#389029</link>
      <author>us</author>
      <description>Steve Amphlett:&lt;br&gt;
&amp;lt;SNIP down to typical even/odd algorithm...&lt;br&gt;
&lt;br&gt;
&amp;gt; mod(x,2)&lt;br&gt;
&amp;gt; 0 if even, 1 if odd&lt;br&gt;
&lt;br&gt;
% now, since even/odd should be integers,&lt;br&gt;
% this may fail, eg,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n=-4:4;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m=n;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;even=mod(m,2)==0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;odd=~even;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[m;even;odd]&lt;br&gt;
% if the user is not careful&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m=pi*n;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;even=mod(m,2)==0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;odd=~even;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[m;even;odd]&lt;br&gt;
% a better way may be this&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;odd=bitand(abs(n),1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;even=~odd;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[n;even;odd]&lt;br&gt;
% ...which would give an error early on&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;odd=bitand(abs(m),1);&lt;br&gt;
&lt;br&gt;
just a thought&lt;br&gt;
us&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</description>
    </item>
    <item>
      <pubDate>Tue, 19 Feb 2008 21:26:02 -0500</pubDate>
      <title>Re: Even/odd</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155034#416177</link>
      <author>Amber York</author>
      <description>How about &lt;br&gt;
&lt;br&gt;
function [ise] = iseven(x);&lt;br&gt;
%works for sccalar and vector&lt;br&gt;
ise = roundn(x/2,0) == x/2;</description>
    </item>
    <item>
      <pubDate>Tue, 19 Feb 2008 22:52:37 -0500</pubDate>
      <title>Re: Even/odd</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155034#416191</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fpfhha$86m$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Amber York &amp;lt;yorksea@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;How about &lt;br&gt;
&lt;br&gt;
&amp;gt;function [ise] = iseven(x);&lt;br&gt;
&amp;gt;%works for sccalar and vector&lt;br&gt;
&amp;gt;ise = roundn(x/2,0) == x/2;&lt;br&gt;
&lt;br&gt;
roundn is only in the Mapping toolbox.&lt;br&gt;
&lt;br&gt;
Please check the function for a full range of input,&lt;br&gt;
such as  iseven([-inf -2 -1 0 1 1.49 1.5 1.51 1.99 2 inf nan])&lt;br&gt;
&lt;br&gt;
I do not have roundn to check with, but I suspect you will&lt;br&gt;
find that your iseven function will tell you that -inf and inf&lt;br&gt;
are even.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Q: Why did the chicken cross the Mobius strip?&lt;br&gt;
&lt;br&gt;
A: There were manifold reasons.</description>
    </item>
    <item>
      <pubDate>Sat, 16 Aug 2008 01:42:01 -0400</pubDate>
      <title>Re: Even/odd</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155034#510859</link>
      <author>Andy Robb</author>
      <description>&amp;gt; % a better way may be this&lt;br&gt;
&amp;gt;      odd=bitand(abs(n),1);&lt;br&gt;
&lt;br&gt;
Is the domain of bitand() limited to 32-bit integers? At&lt;br&gt;
least mod(x,2) covers the range of whole numbers up to 53&lt;br&gt;
bits. Sure, the returned matrix ought to be sparse, omitting&lt;br&gt;
values where it is not certain if the number was even or odd&lt;br&gt;
(e.g. either the fraction is not zero or the value is too&lt;br&gt;
large to be odd).</description>
    </item>
    <item>
      <pubDate>Sat, 16 Aug 2008 11:35:24 -0400</pubDate>
      <title>Re: Even/odd</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155034#510880</link>
      <author>us</author>
      <description>&quot;Andy Robb&quot;&lt;br&gt;
&amp;lt;SNIP bits and pieces...&lt;br&gt;
&lt;br&gt;
&amp;gt; &amp;gt; % a better way may be this&lt;br&gt;
&amp;gt; &amp;gt; =A0 =A0 =A0odd=3Dbitand(abs(n),1);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Is the domain of bitand() limited to 32-bit integers? At&lt;br&gt;
&amp;gt; least mod(x,2) covers the range of whole numbers up to 53&lt;br&gt;
&amp;gt; bits...&lt;br&gt;
&lt;br&gt;
so does the bitxxx family of stock functions...&lt;br&gt;
the limit is&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;help bitmax;&lt;br&gt;
&lt;br&gt;
% the advantage of the BITs...&lt;br&gt;
% they complain if something goes wrong&lt;br&gt;
% and dont let you believe your result is correct as with REM/MOD&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bitand(bitmax,1)&lt;br&gt;
% ans =3D&lt;br&gt;
% these will error out&lt;br&gt;
%    bitand(pi,1)&lt;br&gt;
%    bitand(bitmax+1,1);&lt;br&gt;
% whereas these don't care to warn you&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b=3Dbitmax+(1:5); % valid doubles&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mod(b,2)&lt;br&gt;
% ans =3D 0 0 0 0 0 %!&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rem(b,2)&lt;br&gt;
% ans =3D 0 0 0 0 0 %!&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
  </channel>
</rss>

