Fast interp1 on multiple vectors, same X, but different V and Xq per vector

10 次查看(过去 30 天)
I have many (numVec) 1-D vectors, each of which is sampled at the same many (numSamp) times. I want to do 1-D interpolation for all the vectors. I have a matrix interrogation times (numOutputs x numVec). I want the result to be the the interpolated values also dimension-ed (numOutputs x numVec). I do not need any interpolation across vectors. I do this in an inner loop and would like this to be very fast. Do you know the fastest way to accomplish this?
Simple example:
times_sampled = [0, 1];
data = [1, 2; 5, 6; 20, 21]';
times_interrogated = [.2,.3;.44,.44;.5,.9]';
too_much_data = interp1(times_sampled, data, times_interrogated );
for iVec = 1:size(data, 2)
requested_data(:, iVec) = too_much_data(:, iVec, iVec);
end
Desired Output:
requested_data =
1.2000 5.4400 20.5000
1.3000 5.4400 20.9000
Slow example:
data = rand(10000,512);
times = [0 : size(data, 1) - 1] / size(data, 1);
times_interrogated = rand(1000, 512);
too_much_data = interp1(times, data, times_interrogated );
for iVec = 1:size(data, 2)
requested_data(:, iVec) = too_much_data(:, iVec, iVec);
end
Is there a way to do this faster (without generating all the extra data)? (I tried just looping over interp1, but it was very slow.)
Thanks! -Brian

回答(1 个)

OCDER
OCDER 2018-8-14
The for loop was pretty fast for me. See the timing results below:
data = rand(10000,512);
times = [0 : size(data, 1) - 1] / size(data, 1);
times_interrogated = rand(1000, 512);
requested_data = zeros(size(times_interrogated, 1), size(data, 2));
tic
for iVec = 1:size(data, 2)
requested_data(:, iVec) = interp1(times(:), data(:, iVec), times_interrogated(:, iVec));
end
toc %0.094757 s
requested_data2 = zeros(size(times_interrogated, 1), size(data, 2));
tic
too_much_data = interp1(times(:), data, times_interrogated);
for iVec = 1:size(data, 2)
requested_data2(:, iVec) = too_much_data(:, iVec, iVec);
end
toc %2.813630 s

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by