Effacer les filtres
Effacer les filtres

Can I speed up an ismember operation?

3 vues (au cours des 30 derniers jours)
Christopher
Christopher le 31 Jan 2015
Commenté : Matt J le 1 Fév 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 commentaires
Matt J
Matt J le 1 Fév 2015
Modifié(e) : Matt J le 1 Fév 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 le 1 Fév 2015
gridnum is ideally going to be around 100-1000 and numpoints will always be around 2-25 times that number.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 1 Fév 2015
Modifié(e) : Matt J le 1 Fév 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 commentaires
Christopher
Christopher le 1 Fév 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 le 1 Fév 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by