Alternative for interp1 for fast computation
显示 更早的评论
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 个评论
madhan ravi
2018-11-26
why do you say it's slow?
Chandan Bangalore
2018-11-26
编辑:Chandan Bangalore
2018-11-26
"Is there any alternative way to do this?"
Probably by just calling log(tanh(abs(x))) directly.
I doubt that you will find many methods that are more efficient than calls to highly optimized log, tanh, and abs. Is this line really the major bottle-neck in your code?
It is not clear why you think that interpolation should be faster than these numeric oeprations.
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
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).
采纳的回答
更多回答(1 个)
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
2018-11-26
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
类别
在 帮助中心 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!