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. |