Filter löschen
Filter löschen

Can I speed up an ismember operation?

3 Ansichten (letzte 30 Tage)
Christopher
Christopher am 31 Jan. 2015
Kommentiert: Matt J am 1 Feb. 2015
I have a list of 100 random numbers 1-10:
numpoints = 100;
gridnum = 10;
grid_ind = randi(gridnum,[numpoints 1]); % 100 points in 10 random cells
and each of the 10 possible values has a corresponding list (of random length) of values, which are indexes to grid_ind:
for i=1:gridnum
neighbgrids{i} = randi(numpoints,[randi(5) 1]);
end
So far, so good. Now, I need to find the indexes for all elements in grid_ind which have the same values as in each cell list in neighbgrids. So I use:
for i=1:numpoints
neighblist{i} = find(ismember(grid_ind,neighbgrids{grid_ind(i)}));
end
But this turns out to be very slow. Is there a faster way to do this part?
  5 Kommentare
Matt J
Matt J am 1 Feb. 2015
Bearbeitet: Matt J am 1 Feb. 2015
@Christopher,
Your code takes .006 sec to run on my machine. What speed were you hoping for?
If these are just example numbers, what are the actual typical values of numpoints and gridnum? Is numpoints always much larger than gridnum?
Christopher
Christopher am 1 Feb. 2015
gridnum is ideally going to be around 100-1000 and numpoints will always be around 2-25 times that number.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 1 Feb. 2015
Bearbeitet: Matt J am 1 Feb. 2015
This should speed things up.
for i=1:gridnum
neighblist{i} = find(ismember(grid_ind,neighbgrids{i}));
end
neighblist=neighblist(grid_ind);
I see a 10x speed-up over your originally posted example.
  2 Kommentare
Christopher
Christopher am 1 Feb. 2015
Thanks! This gives me about a 5x speedup the way it is implemented in my code.
Hopefully I can think of a way to avoid ismember all together, in the future, as I was hoping for significantly more speedup.
Matt J
Matt J am 1 Feb. 2015
It seems inefficient to be using find(). The output of ismember is enough to index into grid_inds and then you could maintain neighblist as a matrix instead of a cell, which is more efficient:
neighblist=zeros(gridnum,numpoints);
for i=1:gridnum
neighblist(i,:) = ismember(grid_ind,neighbgrids{i});
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by