[英]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.我有一个 3 维矩阵,想沿该矩阵的第三维插入数据。 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.然而,这些 for 循环不是很优雅和高效,尤其是当矩阵变大时。 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
上调用spline
正是您想要的:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.