Ethunxxx
Ethunxxx

Reputation: 1337

Spline interpolation along one dimension of a matrix in Matlab

I have a 3-dimensional matrix and want to interpolate the data along the third dimension of this matrix. Of course, the following is possible:

I = zeros(3,3,10);
M = rand(3,3,5);
for x = 1:3
    for y = 1:3
        I(x,y,:) = spline(1:5, M(x,y,:), linspace(1,5,10));
    end
end

However, these for loops are not very elegant and efficient, especially when the matrix sizes become bigger. Is there a way to do the spline interpolation along a specific dimension more efficiently?

Upvotes: 1

Views: 319

Answers (1)

Dan
Dan

Reputation: 45741

It looks like calling spline directly on M does exactly what you want:

M = rand(3,3,5);
x = 1:5;
xx = linspace(1,5,10);

%// loop approach
I1 = zeros(3,3,10);
for row = 1:3
    for col = 1:3
        I1(row,col,:) = spline(x, M(row,col,:), xx);
    end
end

%// just calling spline
I2 = spline(x, M, xx);

Test for correctness

>> isequal(I1,I2)

ans =

     1

Upvotes: 1

Related Questions