low speed of 'ismember' function
조회 수: 2 (최근 30일)
이전 댓글 표시
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145223/image.jpeg)
댓글 수: 0
답변 (3개)
Johan Löfberg
2014년 8월 29일
If data is sorted, using ismembc could be a quick fix.
댓글 수: 2
Johan Löfberg
2014년 8월 29일
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
2014년 8월 29일
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
José-Luis
2014년 8월 29일
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
2014년 8월 29일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!