Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fast lookup and replace in large matrices

Subject: Fast lookup and replace in large matrices

From: Casper

Date: 30 Mar, 2012 19:49:12

Message: 1 of 3

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.

Subject: Fast lookup and replace in large matrices

From: Roger Stafford

Date: 30 Mar, 2012 21:36:11

Message: 2 of 3

"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

Subject: Fast lookup and replace in large matrices

From: Casper

Date: 30 Mar, 2012 22:29:29

Message: 3 of 3

You are absolutely right. Thanks alot :)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us