필터 지우기
필터 지우기

Efficient way of finding common elements of two arrays?

조회 수: 4 (최근 30일)
Ivan Shorokhov
Ivan Shorokhov 2015년 1월 8일
댓글: Ivan Shorokhov 2015년 1월 8일
Dear all,
I have two arrays of different size, for example x1=2:1000; and x2=50:1500; I would like to find common elements so my exepcting answer is x3=[50:1000].
Here is the code I'm using right now, but it is too slow.
x1=2:1000;
x2=50:1500;
lx1=length(x1);
lx2=length(x2);
x1 = [x1, zeros(1,length(x2)-length(x1))];
x2 = [x2, zeros(1,length(x1)-length(x2))];
for i=1:length(x2);
for j=1:length(x1);
com(i,j)=x2(i)==x1(j);
end
end
row_com = sum(com);
[~, idx]= find(row_com);
common= x1(idx)
%matchFeatures
How can I do more efficient, preferably without using for loops? Thank you very much for any guidance or advice you could give me.
Regards,
Ivan

채택된 답변

dpb
dpb 2015년 1월 8일
doc intersect % and friends
  댓글 수: 1
Ivan Shorokhov
Ivan Shorokhov 2015년 1월 8일
Thank you very much for the help, the intersect function took only 0.033s to run, so it is more efficient that my code (0.466 s).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by