假设我在Matlab中有以下值的3乘3矩阵:
A = [1 3 5; 3 5 7; 5 7 9];
如何插入我的矩阵,例如:
A = [1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8; 5 6 7 8 9];
因此,在这种情况下,矩阵中添加了“值之间”的所有整数。如何在选择精度的一般情况下执行此操作?例如,如果我的矩阵再次出现:
A = [1 3 5; 3 5 7; 5 7 9];
如何将A插值为:
A = [1 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3 3.2 .........4.8 5; 1.2 1.4 1.6 1.8 2.0 .......... 5.2; 1.4 1.6 .... etc. ];
希望我的问题很清楚=)我想在X和Y方向上插入具有所选精度的“值之间”。
我遇到了 interp2 -function,但我不确定如何使用它并且想如果熟悉这个问题的人可以更快地告诉答案=)
谢谢你的任何帮助!
答案 0 :(得分:3)
来自help interp2
:
ZI = INTERP2(X,Y,Z,XI,YI)插值以找到ZI,其值为 在矩阵XI和YI中的点处的基础2-D函数Z. 矩阵X和Y指定给出数据Z的点。
XI can be a row vector, in which case it specifies a matrix with constant columns. Similarly, YI can be a column vector and it specifies a matrix with constant rows. ZI = INTERP2(Z,XI,YI) assumes X=1:N and Y=1:M where [M,N]=SIZE(Z). ZI = INTERP2(Z,NTIMES) expands Z by interleaving interpolates between every element, working recursively for NTIMES. INTERP2(Z) is the same as INTERP2(Z,1).
因此,对于您的示例,
>> A = [1 3 5; 3 5 7; 5 7 9];
>> A2 = interp2(A, 1) % call of type INTERP2(Z,NTIMES)
A2 =
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
>> [X,Y] = meshgrid(1:0.1:3);
>> A3 = interp(A, X,Y) % call of type INTERP2(Z,XI,YI)
>>
A3 =
1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 ...
1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 2.4000 ...
1.4000 1.6000 1.8000 ...
...