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.

  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?
  • Doug: @Lucy I think this is what you seek to move a line with the mouse in MATLAB. http://blogs.math...

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