Code covered by the BSD License  

Highlights from
Unique Rows for a cell array

4.88889

4.9 | 10 ratings Rate this file 70 Downloads (last 30 days) File Size: 2.98 KB File ID: #25917

Unique Rows for a cell array

by

 

22 Nov 2009 (Updated )

Find unique rows of a cell array containing columns with strings or scalars, or N-D matrices

| Watch this File

File Information
Description

unique(cA,'rows') does not work for cell arrays, the rows entry is ignored (at least as of 2009b). This function handles this case. It works for strings or scalars or both, but each column must contain only one or the other.

MATLAB release MATLAB 7.9 (R2009b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
08 Jul 2014 Mike Shen

Awesome

15 Feb 2013 Tony  
07 Mar 2012 Jessica Lam  
28 Feb 2012 Ulrik

input check not implemented well. eg. if nargin == 2 the it will fail with an error.

28 Feb 2012 Ulrik  
02 Feb 2012 Nate

Great job. Exactly what I was looking for.

27 Jan 2012 ALiveris

Worked for me fast (1s) on a 50000x3 array of strings

20 Oct 2011 Nick

Excellent, and works very fast even on 100,000+ rows.

27 Mar 2010 Sriram

is way faster than the code by Matt Fig for cell arrays of strings

03 Feb 2010 epoxy patch

thanks - exactly what i needed, and it works well

29 Nov 2009 Jim Hokanson

Thanks for the comment Matt. The approach you suggest is definitely faster and more flexible for a small example like the one shown. Unfortunately the approach shown becomes nearly intractable for a larger number of rows when the number of unique rows is also large.

I tried a modified A matrix, that followed the rules outlined in the code. I updated the code tonight to begin to handle N-D matrices (currently they need to be of the same size, although I have a decent idea on how I can change the code to handle mismatches).

A = {1,'red', [3 2;4 5]; ...
2,'green', [4 4;5 6]};

To get more samples I added the following code:
A = repmat(A,[500 1]); %I had 2 rows to begin with
A(:,1) = num2cell(rand(size(A,1),1));

The second line gives the variability and can be commented out to see how large an effect multiple output rows can have.

On my computer the calculation took 30ms for 1000 rows compared to 10 ms with your code. However, if I make the numeric entries random, my code takes about the same amount of time as it had before, but your code takes 2 seconds. For 10000 rows, this changes to approximately 200 seconds (I only it once and got 213) vs 0.2 seconds for my code.

Interestingly, one of the slowest aspects of this code turned out to be cell2mat, which is called by sortrows. I rewrote my own version based a newsthread online. It is a bit frustrating though that TMW doesn't have a really quick way of going from something like {1 2 3} to [1 2 3].

23 Nov 2009 Matt Fig

This is much faster, and has no requirement as to types or ordering:

ii = 1;
while ii<size(A,1)
tf = true(size(A,1),1);
for jj = ii:size(A,1)
if isequal(A(ii,:),A(jj,:)) && ii~=jj
tf(jj) = 0;
end
end
A = A(tf,:);
ii = ii + 1;
end

For example, try it with:
A = {1,'red',magic(3);'blue',magic(4),'green';1,'red',magic(3)}

Updates
29 Nov 2009

Increased speed. Put in support for matrices. Put in options for the usual additional outputs for a "unique" function in Matlab.

Contact us