通过插入相邻元素平均扩展 2*2 矩阵

Expand 2*2 matrix by inserting neighbor elements average

我想写一个转换矩阵的函数x=[a b; c d] 进入另一个矩阵:

y = [  a        (a+b)/2       b   ; 
     (a+c)/2  (a+b+c+d)/4   (b+d)/2 ; 
       c        (c+d)/2       d    ]

您可以使用 2D 卷积计算相邻元素的总和(每个元素的分子),然后使用 2D 卷积与 1 的矩阵确定分母(相邻元素的数量)。

x = [1, 2; 3, 4];

numerator = conv2(x, ones(2));

%    1    3    2
%    4   10    6
%    3    7    4

denominator = conv2(ones(size(x)), ones(2));

%   1   2   1
%   2   4   2
%   1   2   1

result = numerator ./ denominator;

%   1.0000   1.5000   2.0000
%   2.0000   2.5000   3.0000
%   3.0000   3.5000   4.0000

或单线:

result = conv2(x, ones(2)) ./ conv2(ones(size(x)), ones(2));

这也与 bi-linear interpolation 相同,因此您还可以执行以下操作:

[xx,yy] = meshgrid(1:0.5:size(x, 2), 1:0.5:size(x, 1));
result = interp2(x, xx, yy, 'linear');

这两种方法都有适用于任何大小的额外好处 x

您可以按如下方式使用mean函数:

y = [x(:,1)  mean(x,2)   x(:,2)];
y = [y(1,:); mean(y,1);  y(2,:)];