File Exchange Pick of the Week

December 19th, 2008

Counting Elements

Jiro's pick this week is COUNTMEMBER by Jos.

A while back, I wrote a Pick of the Week post on a file that created a MATLAB file template. In that post, I talked a bit about a "best practice" for formatting your MATLAB files: things like, having an H1 line, including help text, including examples, etc.

How is that related to this week's pick? Well, I chose countmember because Jos writes very well-written files. (I could have chosen some of his other files, but I liked the code also.) Open the file up and see it for yourself. He has a simple, but descriptive H1 line so that LOOKFOR will be able to find it. The help block follows the format that many MathWorks MATLAB files use: capitalized syntax (but case-sensitive example code), the examples can be directly executed (highlight and F9), and he has a "See also" section for related functions.

help countmember
  COUNTMEMBER - count members
 
    C = COUNTMEMBER(A,B) counts the number of times the elements of A occurs
    in B, such the Ci = sum(B==Ai). C has the same size and shape as A.
    A may contain non-unique elements. A and B should be of the same type. 
    A can be be a cell array.
 
    Examples:
      countmember([1 2 1 3],[1 2 2 2 2]) 
         -> 1     4     1     0
      countmember({'a','b','c'},{'a','x','a'}) 
         -> 2     0     0
 
    See also ISMEMBER, UNIQUE, HISTC
 
    2005 Jos van der Geest

Of course, the other reason I selected this was his elegant solution to the task of counting the occurrence of elements. The code has a nice error checking mechanism and deals with empty input arguments. The code is vectorized (no for-loops) and uses UNIQUE to eliminate redundant search. Also, notice the little trick to reduce overhead in his lines of code for UNIQUE and ISMEMBER.

Comments?

We will be taking next Friday off for holiday, but we'll be back next year on the 2nd of January. Have a safe and happy holiday!


Get the MATLAB code

Published with MATLAB® 7.7

One Response to “Counting Elements”

  1. Jos replied on :

    I have just updated an improved version of this submission that has less spelling and grammar errors …
    Thanks Jiro for selecting this file for your blog.

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


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

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