Expand and crop the matrix

How can I expand a matrix with zeros around the edge and then crop it to the same size after some manipulations?

+4
source share
2 answers

You can do it:

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 
+3
source

To fill an array, you can use PADARRAY if you have an image processing toolbar.

Otherwise, you can skip and compress the following path:

 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)); 
+3
source

Source: https://habr.com/ru/post/1346376/


All Articles