# interp1 - [Matlab R2016a] - Speed Issues

10 views (last 30 days)
Ilya Klyashtornyy on 30 Jun 2017
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
Walter Roberson on 30 Jun 2017
Would x happen to be equally spaced? Is xq sorted?

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 on 1 Jul 2017
Edited: Ilya Klyashtornyy on 1 Jul 2017
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?

Walter Roberson on 30 Jun 2017
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

Nikolaus Koopmann on 3 Jun 2020
% Current date = June 03, 2020
% Matlab version = 9.6.0.1072779 (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 https://www.mathworks.com/help/matlab/data_analysis/linear-regression.html
% flin = @(x_)p(1)*x_ + p(2); % slow
yq = p(1)*xq + p(2);
end
{"ft_lang_label":"__label__en","ft_lang_prob":0.8507391,"math_prob":0.8033575,"size":2008,"snap":"2023-40-2023-50","text_gpt3_token_len":561,"char_repetition_ratio":0.08982036,"word_repetition_ratio":0.018181818,"special_character_ratio":0.28436255,"punctuation_ratio":0.12686567,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9560225,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-29T23:28:43Z\",\"WARC-Record-ID\":\"<urn:uuid:b262d15b-ebe8-4dbf-b76e-76476c628c3f>\",\"Content-Length\":\"202168\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dc43bffa-c9d6-4692-8ef5-6b4ed62fb78e>\",\"WARC-Concurrent-To\":\"<urn:uuid:57eb63bc-cc7e-4ed8-8ee5-f1514cf1e88a>\",\"WARC-IP-Address\":\"104.68.243.15\",\"WARC-Target-URI\":\"https://se.mathworks.com/matlabcentral/answers/346987-interp1-matlab-r2016a-speed-issues\",\"WARC-Payload-Digest\":\"sha1:YEAUHZV2CHT477W2BUHBB3UL4ADPDOLW\",\"WARC-Block-Digest\":\"sha1:MM664DZ6244MB33KDWY52BNM6ZRQBHHG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510529.8_warc_CC-MAIN-20230929222230-20230930012230-00604.warc.gz\"}"}