ismember function too slow

29 ビュー (過去 30 日間)
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 10 月 17 日
回答済み: Omar Ali Muhammed 2021 年 3 月 3 日
I have to improve the speed of my simulation. Well my code is quite simple. I have a vector A=[1 2 3 4 5 6 7 8 9 10] and another vector B=[3 4 9]. I use ismember to check if every element is into A matrix.
for n=1:length(A)
if ismember(B(n),A)
do-something
end
end
this part of my code is executed so many time in my simulation and my matrices are really big.some idea to improve my code?
  1 件のコメント
Matt J
Matt J 2012 年 10 月 17 日
Since this is speed-critical, it would be wise for you to elaborate on "do-something". That could point the way to eliminating the for-loop altogether, or some other kind of performance optimization.

サインインしてコメントする。

回答 (5 件)

Jonathan Epperl
Jonathan Epperl 2012 年 10 月 17 日
Don't you want to actually INTERSECT A and B? If so, then here's a very fast intersect function that intersects two sets of positive integers only. If you have negative integers you could obviously shift. If you have reals you're SOL. If your integers are greater than ~1e5 the speed gain is negligible.
C = fastintersect(A,B)
if ~isempty(A)&&~isempty(B)
P = zeros(1, max(max(A),max(B)) ) ;
P(A) = 1;
C = B(logical(P(B)));
else
C = [];
end
Obviously you could leave out the check for emtpy sets if you know you won't have empty sets.
  4 件のコメント
Shengtao Wang
Shengtao Wang 2013 年 2 月 20 日
Great! A 50 times speedup for me!
Shengtao Wang
Shengtao Wang 2013 年 2 月 20 日
Note this may produce the same number multiple times if they are not unique in the arrays. not a problem at all though.

サインインしてコメントする。


Matt Fig
Matt Fig 2012 年 10 月 17 日
BinA = B(ismember(B,A));
for n=1:length(BinA)
do-something with BinA(n)
end
  2 件のコメント
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 10 月 17 日
My B vector is a vector that contains ONLY some element of A vector so I don't see any reasons to do what you 're saying to do.
Matt Fig
Matt Fig 2012 年 10 月 17 日
編集済み: Matt Fig 2012 年 10 月 17 日
If your B vector contains ONLY elements of A, then why were YOU checking if each element was a member in your code?? That is just weird.
If you know that every element of B is a member of A, then what is the point of your question?? Why not just get rid of the IF statement in your FOR loop altogether??

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 17 日
編集済み: Azzi Abdelmalek 2012 年 10 月 17 日
use ismember out of the loop
A=[1 2 3 4 5 6 7 8 9 10]
B=[3 4 11 9 22]
idx=find(ismember(B,A)==1)
for k=idx
%do
end
  2 件のコメント
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 10 月 17 日
I have modified my code. the for cycle executes A matrix and not B matrix. is the same thing?
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 17 日
what do you mean?

サインインしてコメントする。


Robert Cumming
Robert Cumming 2012 年 10 月 17 日
if you want to keep it in the loop you can do:
for n=1:length(B)
if min (abs( A-B(n)) ) == 0;
% do something
end
end;
Or use logical indexing
tic
A=[1 2 3 4 5 6 7 8 9 10];
B=[3 4 11 9 22];
idx=find(ismember(B,A)==1);
toc
tic
flags = false(1,length(B));
for i=1:length(A)
check = B==A(i);
flags(check)=1;
end
idx2 = find(flags==1);
toc
A for loop can be faster!!

Omar Ali Muhammed
Omar Ali Muhammed 2021 年 3 月 3 日
A=[1 2 3 4 5 6 7 8 9 10];
B=[3 4 9];
tt=ismember(B,A);
if sum(tt)==3
Do Something;
else
Do Something;
end

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by