No BSD License  

Highlights from
unique_no_sort

3.375

3.4 | 8 ratings Rate this file 16 Downloads (last 30 days) File Size: 1.4 KB File ID: #15209

unique_no_sort

by Caitlin

 

05 Jun 2007 (Updated 07 Jun 2007)

Like unique.m but without sorting

| Watch this File

File Information
Description

Returns the unique elements of a vector, matrix or cell array in the order that they first appear in the input. Does not operate on rows like the original unique.m.

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (15)
06 Jun 2007 Thomas Cunningham

Great idea!

I needed something like this, but for cell arrays of strings, which get alphabetized. Any chance that this is easy to do?

06 Jun 2007 Thomas Cunningham

My apologies. Of all the silly things, I forgot to move the file into my working directory! I'd give it a 5 if I know anything about metyrics, but it worked for what I needed it for so four it is!

06 Jun 2007 Joshua Apgar

Works as advertised, could be faster though.

13 Jun 2007 g s

needs an option to operate on rows like the original matlab UNIQUE function

14 Jun 2007 Joshua Apgar

Thanks for vectorizing the code. It is blazing fast now!

26 Jun 2007 no so good

>> a=magic(1000);
>> tic;unique_no_sort(a);toc
Elapsed time is 0.919820 seconds.
>> tic;unique(a);toc
Elapsed time is 0.313802 seconds.

25 Apr 2008 student student

Thank you Thank you Thank you
You are a good person.

16 Jul 2008 not 4 you to know

just replace lines 97 to 110 with b=a;

>> a=magic(1000);
>> tic;unique_no_sort(a);toc
Elapsed time is 0.058435 seconds.
>> tic;unique(a);toc
Elapsed time is 0.285986 seconds.

16 Jul 2008 not 4 you to know

in unique.m you have to replace those lines...just adding this to make sure. :)
sorry for taking two posts to tell this

29 Sep 2008 reza j

How about something slightly simpler:
function [unsorteduniques ia ib] = uunique(vec)
    vec = vec(:)';
    [v a b] = unique(vec, 'first');
    if nargout > 2
        [ia v] = sort(a);
        [v ib] = ismember(b, v);
    else
       ia = sort(a);
    end
    unsorteduniques = vec(ia);
end

12 Oct 2010 Matthias

thanks, saved me making my own script!

11 Feb 2011 dirt

Found a bug - it sometimes doesn't return the N correctly (I haven't tested it further).

11 Feb 2011 dirt

nevermind ... wishful thinking for something else.

22 Feb 2011 Timothy Kinyanjui

Thanks. Very very useful.

01 Sep 2011 Vanich

Thx so much

Please login to add a comment or rating.
Updates
07 Jun 2007

I vectorized the calculation of 'pos' which results in a significant speed up for large matrices.

Tag Activity for this File
Tag Applied By Date/Time
matrices Caitlin 22 Oct 2008 09:14:39
unique Caitlin 22 Oct 2008 09:14:39
unsorted Caitlin 22 Oct 2008 09:14:39
vector Caitlin 22 Oct 2008 09:14:39
matrix Caitlin 22 Oct 2008 09:14:39
cell array Caitlin 22 Oct 2008 09:14:39

Contact us at files@mathworks.com