Code covered by the BSD License  

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
countmember(A,B)

4.875
4.9 | 8 ratings Rate this file 37 Downloads (last 30 days) File Size: 1.59 KB File ID: #7738 Version: 2.0

countmember(A,B)

by

Jos (10584) (view profile)

 

27 May 2005 (Updated )

Count members.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

C = COUNTMEMBER(A,B) counts the number of times the elements of array A are present in array B, so that C(k) equals the number of occurences of A(k) in B. A may contain non-unique elements. C will have the same size as A.
A and B should be of the same type, and can be cell array of strings.
 
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

MATLAB release MATLAB 8.5 (R2015a)
MATLAB Search Path
/
Other requirements should work on most ML releases (tested for R2015a).
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
09 Jul 2016 Evan

Evan (view profile)

I replaced the line:
N = histc(Loc(:), 1:length(AUnique))

with
N = histcounts(Loc(:), 1:length(AUnique)+1)

based on the recommendation by Mathworks to stop using the histc function. You need to add an extra bin edge because histcounts() doesn't add the extra bin for values exactly equal to the end of the edges vector.

29 Mar 2016 Xiaoyu Bai  
05 Sep 2011 Abhinav Gaur

just what i needed .. Thanks

19 Dec 2008 Pete sherer

Excellent. It could be better if you can also return the logical index as a second output.

05 Dec 2007 Jalal A

many thanks for posting this!

08 Feb 2007 Val cucche

:)

02 Jun 2005 urs (us) schwarz

ery sleek, jos! just a pedestrian thought: replace your dums with the name of the following var, e.g., [AU,dum,j] with [AU,j,j]

31 May 2005 Dimitri Shvorob  
Updates
18 Aug 2005

Reduced overhead by removing unnecessary dummy variables.

21 Dec 2008 1.1

improved help and comments, fixed some spelling issues

14 Apr 2016 2.0

updated from R13

Contact us