Speed up a lookup process

2 次查看(过去 30 天)
YANAN ZHU
YANAN ZHU 2019-3-20
编辑: YANAN ZHU 2019-3-21
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 个评论
Jan
Jan 2019-3-20
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 2019-3-21
编辑:YANAN ZHU 2019-3-21
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

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by