http://nl.mathworks.com/matlabcentral/newsreader/view_thread/318542
MATLAB Central Newsreader  Fast lookup and replace in large matrices
Feed for thread: Fast lookup and replace in large matrices
enus
©19942017 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://nl.mathworks.com/images/membrane_icon.gif

Fri, 30 Mar 2012 19:49:12 +0000
Fast lookup and replace in large matrices
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/318542#871721
Casper
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. <br>
<br>
Specifically, let DATA be the 100.000 by 100.000 matrix. The current initialization for the replacement proceeds initially by:<br>
<br>
map = unique(DATA);<br>
map = [[0:numel(map)1]', map]; <br>
<br>
in other words, existing values in DATA are to be mapped to the range 0:numel(map)1.<br>
<br>
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). <br>
<br>
Obviously this is quite a slow procedure, so I was wondering if anyone had a faster alternative.

Fri, 30 Mar 2012 21:36:11 +0000
Re: Fast lookup and replace in large matrices
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/318542#871732
Roger Stafford
"Casper " <casper.ptrsn@gmail.com> wrote in message <jl52no$9fl$1@newscl01ah.mathworks.com>...<br>
> 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. <br>
> <br>
> Specifically, let DATA be the 100.000 by 100.000 matrix. The current initialization for the replacement proceeds initially by:<br>
> <br>
> map = unique(DATA);<br>
> map = [[0:numel(map)1]', map]; <br>
> <br>
> in other words, existing values in DATA are to be mapped to the range 0:numel(map)1.<br>
> <br>
> 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). <br>
> <br>
> Obviously this is quite a slow procedure, so I was wondering if anyone had a faster alternative. <br>
       <br>
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.<br>
<br>
[~,~,D] = unique(DATA);<br>
DATA = reshape(D1,m,n); % where m = n = 100000<br>
<br>
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 timeconsuming in itself.<br>
<br>
Roger Stafford

Fri, 30 Mar 2012 22:29:29 +0000
Re: Fast lookup and replace in large matrices
http://nl.mathworks.com/matlabcentral/newsreader/view_thread/318542#871737
Casper
You are absolutely right. Thanks alot :)