Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Optimize this!
Date: Fri, 8 Feb 2008 19:31:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 36
Message-ID: <foiall$d61$1@fred.mathworks.com>
References: <5b74607b-c527-4c9b-833a-b08d4be5d48a@s13g2000prd.googlegroups.com> <fogavj$ae4$1@fred.mathworks.com> <fogepf$a4$1@fred.mathworks.com> <fogjdj$r9o$1@fred.mathworks.com> <33Wqj.5343$j95.4025@newsfe3-win.ntli.net>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1202499061 13505 172.30.248.38 (8 Feb 2008 19:31:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 8 Feb 2008 19:31:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:450222


tristram.scott@ntlworld.com (Tristram Scott) wrote in message <33Wqj.5343
$j95.4025@newsfe3-win.ntli.net>...
> 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               
---------
  Tristram, I think it should be emphasized that, as you indicated, the 
efficiency of the code you suggested depends heavily on how densely the 
numbers occur within their range of possible integer values.  For example, if 
you were to start out with a much more sparsely distributed

 vec = ceil(rand(1e6,1)*1e10);

it would require considerably more execution time because of the excessive 
length of your vector x, (not to mention presenting some formidable memory 
problems,) even though the length of vec is no particular problem.

  I still like the use of 'unique' for this kind of problem.  It is more robust in 
that it doesn't depend on the how concentrated the values are, nor whether 
they are integer-valued.

Roger Stafford