low speed of 'ismember' function
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
hi. in my code there is a for loop like the following:
for i=1:n
N=find(~ismember(Vector1,Vector2))
do something...
end
This loop is repeated several thousand times. 'ismember' function is very slow. So that most of the runtime for 'ismember' function lost. Is there any alternative to this?
0 comentarios
Respuestas (3)
Johan Löfberg
el 29 de Ag. de 2014
If data is sorted, using ismembc could be a quick fix.
2 comentarios
Johan Löfberg
el 29 de Ag. de 2014
Then I would scratch ismember and do sort+ismembc (ismember contains a lot of overhead. look into it and you will see that it basically does sort+ismembc once all data is checked etc)
David Sanchez
el 29 de Ag. de 2014
Depending on your data, you might try to code your own function avoiding some conditions and calls to sub-functions within the ismember function.
You can take a look at ismember code with:
type ismember
and try to make it easier.
3 comentarios
José-Luis
el 29 de Ag. de 2014
The way you use ismember() seems to be inefficient. You are going to be processing similar things over and over. You could instead loop through the variable of interest, do an exhaustive search, save that somewhere and then %do something based on those results.
Titus Edelhofer
el 29 de Ag. de 2014
Hi,
does "Vector1" and "Vector2" changes in every iteration? Often you see something like
for i=1:n
v = ismember(x(i), y);
% do something
end
which can be changed to
vAll = ismember(x, y);
for i=1:n
% now do something with vAll(i)
end
Titus
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!