interp1 - [Matlab R2016a] - Speed Issues

10 views (last 30 days)
Heeeey guys,
as I already noticed in this community, I am not alone with that problem. I have tried several solutions proposed in here but it didnt help for reasons like: Old Matlab Versions (interp1 has changed significatly) or another kind of problem.
Let me first tell you what i am doing to make it easier for you to help me:
I a for-loop with approx. 30.000 iterations i use interp1 several time (5 times per iteration - see the profiler below).
Example of the call: [interp1( x, y, xq)]
Here, x is a vector (length 10.000) and y is also a vector (length 10.000) and xq is a vector (length 50.000 or more).
The problem is also that y changes in each iteration and therefore, I cannot precompute any operation (xq is also updated each iteration).
I would be very grateful if you had any suggestions to improve the computational time in this case.
Best regards, Ilya
  1 Comment
Walter Roberson
Walter Roberson on 30 Jun 2017
Would x happen to be equally spaced? Is xq sorted?

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 30 Jun 2017
Test if creating a griddedInterpolant once before the start of the for loop, replacing the Values property of the object each time you change y, is faster.
Ilya Klyashtornyy
Ilya Klyashtornyy on 1 Jul 2017
Edited: Ilya Klyashtornyy on 1 Jul 2017
Hey, thanks for this advice.
I found out that (mb only in my case) creating a new griddedInterpolant takes less time than updating the values.
S=griddedInterpolant(x,y_new): 1.03s
S.Values=y_new: 1.32s
Do you agree with me on that? Do you receive different results?

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 30 Jun 2017
Ilya Klyashtornyy
Ilya Klyashtornyy on 2 Jul 2017
Hey Walter,
thank you very much for your time!
I ve evaluated ther method and compared it to the griddedInterpolant:
%Time per iteration:
griddedInterpolant: average - 0.00328s
your suggestion: average - 0.00672s

Sign in to comment.

Nikolaus Koopmann
Nikolaus Koopmann on 3 Jun 2020
% Current date = June 03, 2020
% Matlab version = (R2019a)
% User name = Nikolaus Koopmann
function [yq,p] = interp1_lin(x,y,xq)
validateattributes(x,{'double'},{'increasing','vector'}) % slow
%% lin. regr.
X = [ones(length(x),1) x(:)];
p = flipud(X\y(:)); % see
% flin = @(x_)p(1)*x_ + p(2); % slow
yq = p(1)*xq + p(2);


Find more on Characters and Strings 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!