简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM