Is there an efficient way to find the indices of the closest matching values in a large matrix, to a large lookup vector?
3 views (last 30 days)
Show older comments
I have 2 large lookup tables (Q_lut , gamma_lut), with each value in them corresponding to the voltages of two voltage vectors (E1_interp for col, E2_interp for row) respectively. I have acquired raw voltage data pairs in large matrices (E1_inst, E2_inst), which I can use to find the associated Q and gamma based the index of the closest matching value in the voltage vectors.
The issue is, that the code below is very slow, taking roughly 1500 s for this small dataset. The only way I could get the min function to work, was to check each value in E1_inst, E2_inst individually. These are 400000x9 matrices, checking through a 25000x1 vector on every iteration so it takes a long time. I'm sorry if similar questions have been asked before, but does anyone know if there is a more efficient way to do this?
Thanks :)
tic
ind_e1 = zeros(size(E1_inst,1), size(E1_inst,2));
ind_e2 = zeros(size(E1_inst,1), size(E1_inst,2));
Q = zeros(size(E1_inst,1), size(E1_inst,2));
gamma = zeros(size(E1_inst,1), size(E1_inst,2));
for i=1:size(E1_inst,1)
for j=1:size(E1_inst,2)
[~, ind_e1(i,j)] = min(abs(E1_interp - E1_inst(i,j)));
[~, ind_e2(i,j)] = min(abs(E2_interp - E2_inst(i,j)));
Q(i,j) = Q_lut(ind_e2(i,j), ind_e1(i,j));
gamma(i,j) = Pitch_lut(ind_e2(i,j), ind_e1(i,j));
end
end
toc
0 Comments
Accepted Answer
Matt J
on 14 Apr 2023
Edited: Matt J
on 14 Apr 2023
[E1,is]=sort(E1_interp);
[E2,js]=sort(Es_interp);
Q_lut=Q_lut(js,is);
Pitch_lut=Pitch_lut(js,is);
[Q,gamma]=deal(zeros(size(E1_inst,1), size(E1_inst,2)));
Q(:)=interp2(E1,E2,Q_lut,E1_inst(:),E2_inst(:),'nearest');
gamma(:)=interp2(E1,E2,Pitch_lut,E1_inst(:),E2_inst(:),'nearest');
More Answers (0)
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!