Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: unique() Help
Date: Tue, 8 Sep 2009 03:43:05 +0000 (UTC)
Organization: Battelle Energy Alliance (INL)
Lines: 23
Message-ID: <h84js9$4ut$1@fred.mathworks.com>
References: <h83aia$5dv$1@fred.mathworks.com> <h83bar$9vu$1@news.eternal-september.org>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1252381385 5085 172.30.248.35 (8 Sep 2009 03:43:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 8 Sep 2009 03:43:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 688530
Xref: news.mathworks.com comp.soft-sys.matlab:568885


With a little prep work, the arrayfun solution can be made faster:

%%%
t = sort(y);
d = [true(1,size(y,2)); 0~=(diff(t))]; % Replace call to unique with index into t.
U = arrayfun(@(x) t(d(:,x),x),1:size(y,2),'Un',0);  % Slow because of anon. func.
%%%


But this is still NOT as fast as the For loop:

%%%
t = sort(y);
d = [true(1,size(y,2)); 0~=(diff(t))]; 
U2 = cell(1,size(y,2));

for ii = 1:size(y,2)
    U2{ii} = t(d(:,ii),ii);  % Replace call to unique with index into t.
end
%%%


For a random array 30-by-2000, the For loop is 4 times faster than the above arrayfun, and 15 times faster than the one line arrayfun approach, at least using 2007b.