Determine and count unique values of an array

Very fast function to determine and count unique values of numeric, logical, char, cell arrays.
5.7K Downloads
Updated 16 Apr 2010

View License

This function determines the unique values of an N-D array, and counts instances of those values using MATLAB's accumarray function, or in cases where accumarray is inappropriate the more traditional sort-diff method is used.

Its primary use is to very quickly count the number of instances of unique values within an array.

However, if only returning unique values (and not counts), it is slightly faster than MATLAB's built-in 'unique' function for arrays of intermediate to large sizes. This is particularly true for integer-valued arrays (not necessarily just integer type). For float arrays, the speed increase is due mainly to fewer input validation tests and other options. Unlike 'unique' it does not have 'rows', 'first', or 'last' options.

It returns the unique values in a sorted vector, and counts of those values are in the same order.

- Usage:
>> [uniques] = count_unique(largeArray);
>> [uniques,numUnique] = count_unique(largeArray);

- Example (a rather trivial example):
>> a = repmat((1:1000),1000,1); %build an unsorted array of values
>> [uniques,numUnique] = count_unique(a);
>> size(uniques)
ans =
1000 1
>> numUnique(1:10)
ans =
1000
1000
1000
1000
1000
1000
1000
1000
1000
1000

- Example (a less trivial example):
>> a = floor(rand(1e6,1)*1e6);
>> [uniques,numUnique] = count_unique(a);
>>uniques(1:10),numUnique(1:10)
ans =
0
1
4
5
7
9
11
12
15
19

ans =
2
2
2
1
2
3
1
1
1
1

For returning only unique values:
- Speed Comparison (integer valued):
>> a = floor(rand(1e7,1)*1e6);
>> tic;[uniques] = count_unique(a);toc %count_unique
Elapsed time is 0.603863 seconds.
>>tic;[uniques] = unique(a);toc %built-in MATLAB
Elapsed time is 2.022784 seconds.

- Speed Comparison (decimal valued):
>>a = rand(1e7,1)*1e6;
>>tic;[uniques] = count_unique(a,'float');toc
Elapsed time is 2.159629 seconds.
>>tic;[uniques] = unique(a);toc
Elapsed time is 2.123154 seconds.

Cite As

Anthony Kendall (2024). Determine and count unique values of an array (https://www.mathworks.com/matlabcentral/fileexchange/23333-determine-and-count-unique-values-of-an-array), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on File Operations 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
1.5.0.0

Speed improvement for large integer arrays.

1.4.0.0

Added check for arrays with large values but few elements, which are not well-suited to accumarray, at suggestion of user.

1.3.0.0

Further modifying description, correcting an error.

1.2.0.0

Added more thorough description and examples, in response to user comment.

1.1.0.0

An improvement to make code more general.

1.0.0.0