Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Fast lookup and replace in large matrices
Date: Fri, 30 Mar 2012 21:36:11 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 22
Message-ID: <jl590b$4a3$1@newscl01ah.mathworks.com>
References: <jl52no$9fl$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1333143371 4419 172.30.248.46 (30 Mar 2012 21:36:11 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 30 Mar 2012 21:36:11 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:762730

"Casper " <casper.ptrsn@gmail.com> wrote in message <jl52no$9fl$1@newscl01ah.mathworks.com>...
> I have a 100.000 x 100.000 matrix containing approximately 62.000 different values between 9 and 567.118. What I'd like to do is to replace all the values with specific counterparts. 
> 
> Specifically, let DATA be the 100.000 by 100.000 matrix. The current initialization for the replacement proceeds initially by:
> 
> map = unique(DATA);
> map = [[0:numel(map)-1]', map]; 
> 
> in other words, existing values in DATA are to be mapped to the range 0:numel(map)-1.
> 
> I then proceed to loop through all 62.000 unique values in DATA, using logical comparison i.e. DATA == map(i,2) to find entries of the i'th value and subsequently replacing the values at the found locations with map(i,1). 
> 
> Obviously this is quite a slow procedure, so I was wondering if anyone had a faster alternative. 
- - - - - - - -
  There is a third argument returned by 'unique', which if you subtract 1 from it and reshape it properly, would itself constitute the newly mapped version of DATA.

 [~,~,D] = unique(DATA);
 DATA = reshape(D-1,m,n); % where m = n = 100000

If this is your sole aim, there would be no need to construct 'map'.  You should note however that 'unique' does a sort which for the size of your DATA should be very time-consuming in itself.

Roger Stafford