Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!z17g2000hsg.googlegroups.com!not-for-mail
From: Pietro <pietromas@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Optimize this!
Date: Fri, 8 Feb 2008 06:39:33 -0800 (PST)
Organization: http://groups.google.com
Lines: 37
Message-ID: <4608e870-32f4-4afb-89d0-4b99981349fe@z17g2000hsg.googlegroups.com>
References: <5b74607b-c527-4c9b-833a-b08d4be5d48a@s13g2000prd.googlegroups.com> 
NNTP-Posting-Host: 87.74.179.93
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1202481574 2588 127.0.0.1 (8 Feb 2008 14:39:34 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Fri, 8 Feb 2008 14:39:34 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: z17g2000hsg.googlegroups.com; posting-host=87.74.179.93; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.9) 
Xref: news.mathworks.com comp.soft-sys.matlab:450128


On 8 Feb, 10:21, tristram.sc...@ntlworld.com (Tristram Scott) wrote:
> John D'Errico <woodch...@rochester.rr.com> wrote:
> > "us " <u...@neurol.unizh.ch> wrote in message
> > <fogepf$a...@fred.mathworks.com>...
> >> "John D'Errico":
> >> <SNIP has a bad hair day...
>
> [snip]
>
> Some interesting discussion.  Assuming that John is on target here, then
> the following is an alternative which works a bit faster for me:
>
> >> vec = ceil(rand(1e6,1)*1e6); % Some random numbers
> >> x = zeros(max(vec)-min(vec)+1,1) + nan; % A mapping vector
> >> x(vec) = 1; % Find the unique values
> >> nu = sum(~isnan(x));% Count them
> >> x(~isnan(x)) = 1:nu; % Index them
> >> y = x(vec); % Use the index in reverse
>
> This does rely on the values all being integers, and does take more than
> one line of code, but depending on the distribution of the values in vec,
> it may give you some improvement in speed.
>
> --
> Dr Tristram J. Scott
> Energy Consultant

Outstanding. Thank you both for two fine solutions. Johns is obviously
the nicer but I agree with Dr. Scott that his is significantly faster,
if pig ugly.

With 500 unique values in vec drawn from the range [1, 1e6], the
unique solution runs in about 0.5 and 9 seconds on a vec of size 1e6
and 1e7 respectively. The ugly solution does it in 0.1 and 0.7
respectively. Nice.

Thanks very much again.