5.0

5.0 | 1 rating Rate this file 27 downloads (last 30 days) File Size: 2.13 KB File ID: #22635

Deleting multiple points: UniqueRows

by Luigi Giaccari

 

09 Jan 2009 (Updated 07 Aug 2009)

Code covered by the BSD License  

UniqueRows can be a faster alternative to the Matlab unique(X,'rows');

Download Now | Watch this File

File Information
Description

The description has been moved to:

http://www.advancedmcode.org/deleting-multiple-points-uniquerows.html

MATLAB release MATLAB 7.6 (R2008a)
Other requirements should work on all platform
Zip File Content  
Other Files license.txt,
UniqueRows.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
14 Jan 2009 John D'Errico

I like that this author has been improving his work from his initial submissions. This function has pretty good help. It has error checks. Lots of informative internal comments. (Well done there.)

I'd suggest a better H1 line, designed to be useful when the lookfor function is used. This should be a descriptive sentence that describes the function and includes intelligently chosen keywords that can be searched for by lookfor. This enables the function to be found next year when you forget the name UniqueRows.

I did a quick test of the code to compare speed. First look at an integer array.

A = round(rand(10000,4)*10);

See that both unique and UniqueRows got the same size results.

B = unique(A,'rows');
B2 = UniqueRows(A);

size(B),size(B2)
ans =
        6891 4
ans =
        6891 4

See that in this case, unique was faster. UniqueRows will probably pick up some time on floating point arrays.

timeit(@() unique(A,'rows'))
ans =
     0.056572

timeit(@() UniqueRows(A))
ans =
     0.071071

What bothers me is that the two codes do not produce the same results. The order is not important. But UniqueRows seems to miss some rows.

[sortrows(B),sortrows(B2)]
ans =
     0 0 1 1 0 0 1 5
     0 0 1 5 0 0 1 5
     0 0 2 5 0 0 2 5
     0 0 2 6 0 0 5 3
     0 0 5 3 0 0 5 4
     0 0 5 4 0 0 5 8
     0 0 5 8 0 0 5 9
     0 0 5 9 0 0 6 0
     0 0 6 0 0 0 6 3
     0 0 6 3 0 0 6 6
     0 0 6 6 0 0 7 1
     0 0 7 1 0 0 8 1
     0 0 7 2 0 0 8 4
     0 0 7 8 0 0 8 8
     0 0 8 1 0 0 10 4
     0 0 8 3 0 1 1 2
     0 0 8 4 0 1 1 8
     0 0 8 8 0 1 2 4
     0 0 9 8 0 1 2 8
     0 0 10 1 0 1 2 8
     0 0 10 4 0 1 3 2
...

For example, is there a row [0 0 2 6] in the original array? In fact, that row was A(260,:).

find(all(bsxfun(@eq,A,[0 0 2 6]),2))
ans =
   260
 
A(260,:)
ans =
     0 0 2 6

In fact, it appears that UniqueRows does not produce truly unique rows, even though it generated the correct number of rows. So there must be an indexing bug in the code.

B3 = unique(B2,'rows');
size(B3)
ans =
        5305 4

I was expecting to give this a good review. I'll hold off for now and look for repairs. With an improved H1 line and this bug fixed, if it is truly more efficient, I'd have given it a high rating. As it is with the bug it needs improvement.

15 Jan 2009 Luigi Giaccari

I apologize for that it was a last minute modification which I thought not to be effetive but it was.
 I am sending an update version which I hope works fine. If you ever found something wrong please aknoledge me.

06 Jul 2009 Óscar J. Rubio Martín

A fast and smart alternative.
Just one comment:
As you want to replace unique(A,'rows'), include the possibility of getting two outputs:
- B: the matrix of unrepeated rows
- ind: the indices of the rows of B in the original matrix A

Please login to add a comment or rating.
Updates
15 Jan 2009

Fixed the bug thanks to John D'Enrico report.

15 Jan 2009

Suppressed the second output, it will be back in future realeses

18 Mar 2009

added link to my website

07 Aug 2009

added link

Tag Activity for this File
Tag Applied By Date/Time
unique Luigi Giaccari 09 Jan 2009 15:34:48
2d Luigi Giaccari 09 Jan 2009 15:34:48
3d Luigi Giaccari 09 Jan 2009 15:34:48
points Luigi Giaccari 09 Jan 2009 15:34:48
image Luigi Giaccari 09 Jan 2009 15:34:48
image processing Luigi Giaccari 09 Jan 2009 15:34:48
mathematics Luigi Giaccari 09 Jan 2009 15:34:48
optimization Luigi Giaccari 09 Jan 2009 15:34:48
signal processing Luigi Giaccari 09 Jan 2009 15:34:48
aerospace Luigi Giaccari 09 Jan 2009 15:34:48
automotive Luigi Giaccari 09 Jan 2009 15:34:48
data import Luigi Giaccari 09 Jan 2009 15:34:48
data export Luigi Giaccari 09 Jan 2009 15:34:48
delaunay Luigi Giaccari 09 Jan 2009 15:34:48
array Luigi Giaccari 09 Jan 2009 15:34:48
sort Luigi Giaccari 09 Jan 2009 15:34:48
sortrows Luigi Giaccari 09 Jan 2009 15:34:48
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com