Can I speed up an ismember operation?

3 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2015-2-1
编辑:Matt J 2015-2-1
@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 2015-2-1
gridnum is ideally going to be around 100-1000 and numpoints will always be around 2-25 times that number.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2015-2-1
编辑:Matt J 2015-2-1
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 个评论
Christopher
Christopher 2015-2-1
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 2015-2-1
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by