필터 지우기
필터 지우기

what is the fastest way to search a huge matrix

조회 수: 9 (최근 30일)
Li Xue
Li Xue 2016년 2월 12일
댓글: Guillaume 2016년 2월 12일
I have 2 huge matrices, A and B, with the same size (9000 by 9000). I need to search two numbers x and y from A and B, respectively, and return the indices where x and y appears in A and B. I have 10,000+ pairs of x and y saved in a table of C, therefore need to do 10,000+ such searches.
I am currently using the following code, but it took me 4+ hours to finish the searching.
linearIdx=rowfun(@(x, y)find(A==x & B == y),C ) ;
[I,J]=ind2sub(size(A), linearIdx.Var1);
Is there faster way to do it? Maybe I should save A and B into hashtable? Thanks.

채택된 답변

Guillaume
Guillaume 2016년 2월 12일
편집: Guillaume 2016년 2월 12일
I'm assuming that the (x, y) pair is always present and only present once in (A, B). Otherwise the code you've posted would fail at the rowfun point.
The best way to find the locations of all the elements of an array within another array is to use ismember. In your case, the best thing to do would be to reshape A and B into columns, concatenate them into a 2 column array, and perform the search with ismember plus the 'rows' option:
[isrowfound, linearidx] = ismember(C, table(A(:), B(:)), 'rows');
%note: I wouldn't bother with tables and instead just have C as a matrix, in which case:
%[isrowfound, linearidx] = ismember(C, [A(:), B(:)], 'rows');
assert(all(isrowfound), 'Some rows in C were not present in (A, B)')
[I, J] = ind2sub(size(A), linearidx);
If a pair (x, y) is not found then the call to sub2ind will fail (due to linearidx containing 0).
If a pair (x, y) is found several time, you'll only get the location of the first one. Unlike your code, it won't cause an error.
  댓글 수: 2
Li Xue
Li Xue 2016년 2월 12일
편집: Li Xue 2016년 2월 12일
Many thanks. The code is now only taking seconds to run! Thanks a lot! Yes, (x, y) pair is always present and only present once in (A, B). By the way, you had a typo in your code: sub2ind should be ind2sub.
Guillaume
Guillaume 2016년 2월 12일
Do'h. For some reason I always swap these two. I've corrected my original post.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by