4
如何在边缘周围用零填充矩阵,然后在一些操作后将其裁剪回相同的大小?展开并裁剪矩阵
如何在边缘周围用零填充矩阵,然后在一些操作后将其裁剪回相同的大小?展开并裁剪矩阵
你可以这样做:
octave:1> x = ones(3, 4)
x =
1 1 1 1
1 1 1 1
1 1 1 1
octave:2> y = zeros(rows(x)+2, columns(x)+2);
octave:3> y(2:rows(x)+1, 2:columns(x)+1) = x
y =
0 0 0 0 0 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 0 0 0 0 0
octave:4> y = y.*2 (manipulation)
y =
0 0 0 0 0 0
0 2 2 2 2 0
0 2 2 2 2 0
0 2 2 2 2 0
0 0 0 0 0 0
octave:5> x = y(2:rows(x)+1, 2:columns(x)+1)
x =
2 2 2 2
2 2 2 2
2 2 2 2
要垫一个数组,你可以使用PADARRAY,如果您有图像处理工具箱。
否则,你可以垫和收缩方式如下:
smallArray = rand(10); %# make up some random data
border = [2 3]; %# add 2 rows, 3 cols on either side
smallSize = size(smallArray);
%# create big array and fill in small one
bigArray = zeros(smallSize + 2*border);
bigArray(border(1)+1:end-border(1),border(2)+1:end-border(2)) = smallArray;
%# perform calculation here
%# crop the array
newSmallArray = bigArray(border(1)+1:end-border(1),border(2)+1:end-border(2));
我的例子扩大和作物根据OP的规格任意大小的矩阵。如果你错过了,我想你可能已经快速阅读了我的答案。 – 2011-05-05 01:57:34