Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab中不同时间的矩阵插值_Matlab_Grid_Interpolation - Fatal编程技术网

Matlab中不同时间的矩阵插值

Matlab中不同时间的矩阵插值,matlab,grid,interpolation,Matlab,Grid,Interpolation,我计算了存储在特定时间向量矩阵中的变量。 现在我想在这些新时间向量的整个矩阵之间插值,以得到所需新时间向量的矩阵 我提出了以下解决方案,但它似乎笨重且需要计算: clear all; a(:,:,1) = [1 1 1;2 2 2;3 3 3]; % Matrix 1 a(:,:,2) = [4 4 4;6 6 6;8 8 8]; % Matrix 2 t1 = [1 2]; % Old time vector t2 = [1 1.5 2]; % New time vector % In

我计算了存储在特定时间向量矩阵中的变量。 现在我想在这些新时间向量的整个矩阵之间插值,以得到所需新时间向量的矩阵

我提出了以下解决方案,但它似乎笨重且需要计算:

clear all;

a(:,:,1) = [1 1 1;2 2 2;3 3 3]; % Matrix 1
a(:,:,2) = [4 4 4;6 6 6;8 8 8]; % Matrix 2

t1 = [1 2]; % Old time vector

t2 = [1 1.5 2]; % New time vector

% Interpolation for each matrix element
for r = 1:1:size(a,2)
for c = 1:1:size(a,1)
tab(:) = a(r,c,:);
tabInterp(r,c,:) = interp1(t1,tab(:),t2);
end
end
结果应该是:

[2.5000    2.5000    2.5000
    4.0000    4.0000    4.0000
    5.5000    5.5000    5.5000]

有什么想法吗?

我不认为基于循环的方法有什么问题,但是如果您正在寻找一种无循环的方法,您可以执行以下操作

[rows, cols, ~] = size(a);
aReshape = reshape(a, rows*cols, []).';
tabInterp = reshape(interp1(t1, aReshape, t2).', rows, cols, []);

查看
interp1
的源代码,似乎仍在使用
for
循环,因此我怀疑这是否会带来任何性能提升。

您可以手动执行线性插值,并且一次完成所有操作

m = ( t2 - t1(1) ) / ( t1(2) - t1(1) );  
% Linear interpolation using the standard 'y = m*x + c' linear structure
tabInterp = reshape(m,1,1,[]) .* (a(:,:,2)-a(:,:,1)) + a(:,:,1);
这将适用于任何大小的
t2
,只要
t1
有2个元素

如果您的
t1
包含2个以上的元素,则可以使用
interp1
创建缩放向量
m
。这是相对有效的,因为您只对时间向量使用
interp1
,而不是矩阵:

m = interp1( t1, (t1-min(t1))/(max(t1)-min(t1)), t2, 'linear', 'extrap' );


这将使用带有
*
操作的隐式扩展,该操作需要R2016b或更高版本。如果您使用的是较旧的MATLAB版本,请使用
bsxfun
实现相同的功能。

@jodag我已经更新了答案并解决了这个问题,现在可以使用多值
t1
一次性完成。感谢您的宝贵意见。它工作得完美无缺。我也尝试过使用interp3。你认为什么更快?清除所有;a(:,:,1)=[11;22;33];%矩阵1A(:,:,2)=[4;6 6;8 8];%矩阵2 t1=[12];%旧时间向量t2=[1.5 2];%每个矩阵元素的新时间向量%插值[X,Y,Z]=meshgrid(1:size(a,1),1:size(a,2),t1);[X2,Y2,Z2]=meshgrid(1:size(a,1),1:size(a,2),t2);tabInterp=interp3(X,Y,Z,a,X2,Y2,Z2);你为什么不
timeit
看看呢?如果您想问一个相关但不同的性能问题,可以将其作为一个新问题而不是在评论中提问:)