Alternative for interp1 for fast computation

조회 수: 19 (최근 30일)
Chandan Bangalore
Chandan Bangalore 2018년 11월 26일
댓글: Michal 2021년 7월 28일
I am working with matrices of size (1000 x 1000) and have a function that involves log and tanh functions. To avoid the computational complexity I want to store the results in the form of a lookup table and access them directly without performing log(tanh(abs(x))) everytime. I am using interp1 function to do this, but this is very slow. Could someone please help me speed up the below function?
range = 0:0.1:6;
data = log(tanh(abs(range)));
value = [0 0.1 0.2105];
out = interp1(range,data,value,'nearest');
  댓글 수: 5
David Thoen
David Thoen 2020년 11월 16일
might be a bit late, but check out this piece of interpolation-script: https://www.mathworks.com/matlabcentral/fileexchange/28376-faster-linear-interpolation
Michal
Michal 2021년 7월 28일
Yes the FEX https://www.mathworks.com/matlabcentral/fileexchange/28376-faster-linear-interpolation is definitely fastest solution ... based on matlab scripts (without MEX).

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2018년 11월 27일
편집: Jan 2018년 11월 27일
You can try this C-Mex function for a linear interpolation: https://www.mathworks.com/matlabcentral/fileexchange/25463-scaletime
range = 0:0.1:6;
data = log(tanh(range)); % No abs() needed for the given range
value = linspace(0, 6, 1e6); % Equivalent to 1000x1000 matrix
tic;
index = value * (numel(range)-1) / 6 + 1;
out = ScaleTime(data, index);
toc
tic
out2 = log(tanh(value));
toc
  댓글 수: 7
Bruno Luong
Bruno Luong 2018년 11월 29일
step = 0.1;
index = floor((value - range(1))/step) + 1;
Jan
Jan 2018년 11월 29일
range = 0.1:6;
data = -log(tanh(range));
value = [-0.5, 0.5];
index = abs(value) * (numel(range)-1) / 6 + 1;
out = ScaleTime(data, index);
You need to create the look-up-table data only for the positive inputs, if you provide positive inputs only.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2018년 11월 26일
range = 0:0.1:6;
data = log(tanh(abs(range)))
edges = [-Inf, 0.5*(range(1:end-1)+range(2:end)), Inf]; % this is done once
value = linspace(0,6,1024);
% This replace INTERP1
[~,~,loc]=histcounts(value,edges);
out = data(loc)
  댓글 수: 2
Chandan Bangalore
Chandan Bangalore 2018년 11월 26일
This one is definitely faster than interp1 function but still not better than directly using the log(tanh(abs(x))) function.
I just tried the below sample code to check the speed.
tic;
for i = 1:1000000
range = 0:0.1:6;
data = -log(tanh(abs(range)));
value = [0 0.1 0.2105];
out1 = interp1(range,data,value,'nearest');
end
toc;
tic;
for i = 1:1000000
range = 0:0.1:6;
data = -log(tanh(abs(range)));
edges = [-Inf, 0.5*(range(1:end-1)+range(2:end)), Inf]; % this is done once
value = [0 0.1 0.2105];
% This replaces INTERP1
[~,~,loc]=histcounts(value,edges);
out2 = data(loc);
end
toc;
tic;
for i = 1:1000000
value = [0 0.1 0.2105];
out3 = -log(tanh(abs(range)));
end
toc;
Bruno Luong
Bruno Luong 2018년 11월 26일
편집: Bruno Luong 2018년 11월 26일
Sorry you timing is flawed for 2 reasons:
  • includes the non-relevant parts
  • too small data therefore overhead of histcounts and interp1 will kills any advantage

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by