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