MATLAB Answers

Speed up a lookup process

1 view (last 30 days)
YANAN ZHU
YANAN ZHU on 20 Mar 2019
Edited: YANAN ZHU on 21 Mar 2019
Hi, Guys,
I have been trying to speed up a lookup process.
A is a array of 1 million numbers. B is a array of 15 thousands numbers (no duplicated numbers in B). I am trying to do is: for every number in A, find the index of same number in B and use that index to fetch a value in array C (which is of the same size as A).
The look up process stated above has to be repeated several thousands times since I have thousands of different array B to loop up, but A and C kept always same.
I used ismember function AND gpuArray, which gave me some improvement. I wonder if there still some space for improvement. Let me know you suggestions!
Here is some testing code that I used:
A=randperm(1e6);
A=gpuArray(A);
C=randperm(1e6);
C=gpuArray(C);
tic
for j=1:10 % e.g. 10 loops with different B (all integers) and C, but A (all integers) kept same in every loop
B=randperm(15*1e3);
B=gpuArray(B);
[~,Locb] = ismember(A,B);
Locb(Locb==0)=size(B,1)+1;%%% make all the zero in Locb to be a element that is indexable
C(size(B,1)+1,1)=0;% add zero in the end of the table;
D(j,1)=sum(C(Locb));
end
toc
YZ
  2 Comments
YANAN ZHU
YANAN ZHU on 21 Mar 2019
Hi Jan,
Thanks for your answer. I edit the posted code.
Remove 'rows' flag speed up the code a lot.
A and B does not contain NaNs, I would appreciate if you could share your much faster indexing approach.
Yanan

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!