Version 2.1 (1.87 KB) by Jos (10584)
Count members.
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.

countmember([1 2 1 3],[1 2 2 2 2])
-> 1 4 1 0
-> 2 0 0

Cite As

Jos (10584) (2024). countmember (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Characters and Strings in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

using histcounts now

updated from R13

improved help and comments, fixed some spelling issues

Reduced overhead by removing unnecessary dummy variables.