interp1 - [Matlab R2016a] - Speed Issues
조회 수: 8 (최근 30일)
이전 댓글 표시
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
채택된 답변
Steven Lord
2017년 6월 30일
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.
댓글 수: 4
추가 답변 (2개)
Walter Roberson
2017년 6월 30일
There are several faster interp1 in the file exchange, including
댓글 수: 6
Walter Roberson
2017년 7월 1일
Under the assumption that xq might be "exactly" -pi but is less than +pi:
xmin = -pi; xmax = +pi; span = xmax - xmin;
idx = floor( (xq - xmin) * length(x) / span + 1 );
offset = xq - x(idx);
yq = y(idx) * (1-offset) + y(idx+1) * offset;
I would need to double-check that the rounding for the index calculation works out well enough.
The idea is that with x being equally spaced with known min and max, then algebraically you can calculate the index into the x vector through a simple scaling operation. Then you can use the index vector to look up the values to do linear interpolation.
Nikolaus Koopmann
2020년 6월 3일
% 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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!