Speed up a lookup process

3 views (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
Jan
Jan on 20 Mar 2019
Why do you use the 'rows' flag, if the inputs are column vectors?
The detail "use that index to fetch a value in array C (which is of the same size as A)" is not clear: "C" does not appear in your posted code.
"gave me some improvemen" - compared to what?
The values matter: Are they integer values? Is B e.g. randperm(15e3)? Then a much faster indexing approach is possible. Do A or B contain NaNs?
Please post a short meaningful example for the inputs.
YANAN ZHU
YANAN ZHU on 21 Mar 2019
Edited: 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!