File Exchange

image thumbnail

countmember

version 2.1 (1.87 KB) by Jos (10584)
Count members.

14 Downloads

Updated 20 Jan 2019

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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

Cite As

Jos (10584) (2019). countmember (https://www.mathworks.com/matlabcentral/fileexchange/7738-countmember), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Yindi Zhao

Evan

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.

Xiaoyu Bai

just what i needed .. Thanks

Pete sherer

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

Jalal A

many thanks for posting this!

Val cucche

:)

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]

Dimitri Shvorob

Updates

2.1

using histcounts now

2.0.0.0

updated from R13

1.1.0.0

improved help and comments, fixed some spelling issues

1.0.0.0

Reduced overhead by removing unnecessary dummy variables.

Reduced overhead by removing unnecessary dummy variables.

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux