Rank: 87305 based on 0 downloads (last 30 days) and 0 file submitted



Personal Profile:


Watch this Author's files


Comments and Ratings by Kristi
Updated File Comments Rating
15 Apr 2010 Determine and count unique values of an array Very fast function to determine and count unique values of numeric, logical, char, cell arrays. Author: Anthony Kendall

Inside int_log_unique, instead of maxVal you should calculate the size of the accumarray output matrix as
maxIndex = max(x(:)) - min(x(:)) + 1;
since x may have all negative integers.

Also, the int_log_unique code might be slightly faster if you don't make the second call to accumarray, and you should always subtract out the minVal to make accumarray more efficient.

function [uniques,numUnique] = int_log_unique(x,nOut)
%First, determine the offset for negative values
minVal = min(x(:));
%Check to see if accumarray is appropriate for this function
maxIndex = max(x(:)) - minVal + 1;

if maxIndex / numel(x) > 1000
error('Accumarray is inefficient for arrays when ind values are >> than the number of elements')

%Now, offset to get the index
index = x(:) - minVal + 1;

%Count the occurrences of each index value
numUnique = accumarray(index,1);

%Get the values which occur more than once
uniques = z(numUnique>0) + minVal - 1;

if nOut == 2
%Trim the numUnique array
numUnique = numUnique(numUnique>0);

Contact us