# Speed up a lookup process

1 ビュー (過去 30 日間)
YANAN ZHU 2019 年 3 月 20 日

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 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 2019 年 3 月 21 日
Hi Jan,
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

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!