KatyB
KatyB

Reputation: 3990

faster method of interpolation in matlab

I am using interp1 to inteprolate some data:

temp = 4 + (30-4).*rand(365,10);
depth = 1:10;

dz = 0.5; %define new depth interval
bthD = min(depth):dz:max(depth); %new depth vector

for i = 1:length(temp);
    i_temp(i,:) = interp1(depth,temp(i,:),bthD);
end

Here, I am increasing the resolution of my measurements by interpolating the measurements from 1 m increments to 0.5 m increments. This code works fine i.e. it gives me the matrix I was looking for. However, when I apply this to my actual data, it takes a long time to run, primarily as I am running an additional loop which runs through various cells. Is there a way of achieving what is described above without using the loop, in other words, is there a faster method?

Upvotes: 2

Views: 2216

Answers (1)

Dan Becker
Dan Becker

Reputation: 1216

Replace your for loop with:

i_temp = interp1(depth,temp',bthD)';

You can get rid of the transposes if you change the way that temp is defined, and if you are OK with i_temp being a 19x365 array instead of 365x19.

BTW, the documentation for interp1 is very clear that you can pass in an array as the second argument.

Upvotes: 7

Related Questions