Determine and count unique values of an array

版本 1.5.0.0 (2.5 KB) 作者: Anthony Kendall
Very fast function to determine and count unique values of numeric, logical, char, cell arrays.
5.7K 次下载
更新时间 2010/4/16

查看许可证

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.

引用格式

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. 检索来源 .

MATLAB 版本兼容性
创建方式 R2008b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
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