Loren on the Art of MATLAB

August 13th, 2009

Evens and Odds

There are so many functions in MATLAB that sometimes users think that if they can't find the function they are thinking of, it's because they don't know the name. Sometimes that's true, and sometimes it's because there's a quick, easy way to get the task done. There are relatively frequent questions on the MATLAB newsgroup regarding how to determine if numbers are even or odd.

The most frequent answers involve people pointing to functions such as rem and mod. However, on these same threads, you will frequently see Urs Schwarz warn about the limitations of those functions for the purpose of determining even/odd-ness. He has made a contribution, isodd on the File Exchange that encapsulates the algorithm he prefers, based on he functions bitmax and bitand The main issues Urs handles that aren't fully handled with solutions based on rem or mod include non-integer inputs and doubles larger than the largest floating point integer.

If you do lots of error checking already, and know that your data is within acceptable bounds, you are probably fine using an approach based on rem or mod. Do you have applications where an enhanced version, such as the one Urs has posted, is important? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.8

3 Responses to “Evens and Odds”

  1. Ben replied on :

    One function that I often have to be careful with, is using HIST to count unique members of an array:

    [n,u]=hist(x,unique(x));
    

    This works fine unless x has only one unique element. There is a good submission on FEX that solves this problem but it would be nice to see a built-in version.

    Also, anyone working with large integers (bigger than bitmax) should look at John D’Errico’s marvellous Variable Precision Integer toolbox.

  2. Emilie S. replied on :

    This is very impressive indeed. Urs Schwarz’s contribution appears very genius and I will be looking in to this isodd.

  3. Joachim replied on :

    Very interesting post, Loren. Unfortunately, I don’t have an example application.

    Just wanted to point out that it appears a bitwise logical AND comparison would be more efficient than a modulus operation. To compute the modulus, you have to do a division which tends to be costly computationally… So I find Urs’ code appealing from that perspective. Not 100% sure about Matlab though. What I have in mind is that, AFAIK, C compilers optimize (x%2) to (x&1) if the two are equivalent on the architecture.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • Jun: I totally can not believe it, Loren. You are really helpful. Thank you so much, MATLAB master!
  • Loren: Wow folks- Always lots of interest when there’s a quickie to try out! I will only make 2 general...
  • Loren: Jun- ismember is your friend here: >> [aa,ind] = ismember(Array2,Arra y1) aa = 1 1 1 1 1 1 1 ind = 1 2 1 4 4 3...
  • Dan: I like the first way better than the second way. Combining the arrays into one and running any is nice, although...
  • James Myatt: How about I = (a == 0 | b == 0); a(I) = []; b(I) = [];
  • Tunc: Hello Loren, love your blog because of such inspiring and challenging comments to such ’small’...
  • Pekka Kumpulainen: Here is my tradeoff. I usually want to keep the original variables as they are most probably...
  • Iain: Followup: Of course, to allow NaNs (counting them as non-zero): mask = (a~=0) & (b~=0); The mask says “a...
  • Matt Fig: I would usually go with something like this: y = a&b; x = a(y); y = b(y); But I was surprised to find...
  • kk: c=all([a;b]) a(c) a(b)

These postings are the author's and don't necessarily represent the opinions of The MathWorks.