Searching for a string in a very efficient way

조회 수: 3 (최근 30일)
gwar
gwar 2013년 7월 17일
Hi.
I have two cell arrays, A and B. A contains 3000 strings and B contains 107,000 strings. Some of the strings in B might also be present in A. Each of these strings is a 7 letter word. I need to find which strings in B are also present in A. I did this by writing the following:
[~,p] = ismember(A, B);
p(p==0) = []; % removing zeros from the p array
I then use the indices in p to get the strings in B that are also in A. This works but takes a long time to compute (0.4 sec). Since I need to do this step about 30 times (for other A and B), the overall time becomes large. Does anyone have an idea of how to make this string search faster. With thanks in advance, G

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 17일
편집: Azzi Abdelmalek 2013년 7월 17일
Instead of ismember(B, A), You should use
ismember(A,B);
  댓글 수: 1
gwar
gwar 2013년 7월 17일
In my case ismember(A,B) or ismember(B,A) have very similar computation times. I have edited my question since I mixed up the A and B.

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

추가 답변 (1개)

Jan
Jan 2013년 7월 17일
ismember sorts the input strings to allow for an efficient binary search. If you call this 30 times, sorting the larger cell string explicitly will save 29 sorting procedures.
  댓글 수: 1
gwar
gwar 2013년 7월 17일
You mean combining all the 30 A´s and 30 B´s and calling ismember only once on the combined cell array?

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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by