Moving a small matrix inside a larger matrix in MATLAB

Suppose A is a 5x5 matrix of zeros:

>> A = zeros(5)

A =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0

And B is a small matrix of them (2x2):

>> B = ones(2)

B =

     1     1
     1     1

Now I am looking for 16 different cases that represent matrices from C1, C2, C3, ..., C16

What are:

C1 =                                

     1     1     0     0     0
     1     1     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0

C2 =                                

     0     0     0     0     0
     1     1     0     0     0
     1     1     0     0     0
     0     0     0     0     0
     0     0     0     0     0



C3 =                                

     0     0     0     0     0
     0     0     0     0     0
     1     1     0     0     0
     1     1     0     0     0
     0     0     0     0     0

... and finally C16equal to:

C16 =                                

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     1     1
     0     0     0     1     1

As you can see, this looks like a smaller matrix (B) when moving inside the larger one (A).

Many thanks,

+4
source share
3 answers

You can achieve what you want by using circshift(...)the appropriate rows and columns to shift the values ​​relative to the Matrix . The example you mentioned is the example shown in the "Move Matrix Elements" section of a 4x4 matrix page.

Take for example

A = [1 1 0 0; 1 1 0 0; 0 0 0 0; 0 0 0 0]
A =

     1     1     0     0
     1     1     0     0
     0     0     0     0
     0     0     0     0
Y = circshift(A,[1 1])
Y =

     0     0     0     0
     0     1     1     0
     0     1     1     0
     0     0     0     0

- Mathworks , , , , . , 16 5x5, , ,

EDITED: 5x5x16 C

A=zeros(5,5);
A(1:2,1:2)=1
c=1;C=zeros(5,5,16);
for i=0:3
    for j=0:3
        C(:,:,c)=circshift(A,[i j])
        c=c+1;
    end
end

( )

A =

     1     1     0     0     0
     1     1     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


ans =

     1     1     0     0     0
     1     1     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


ans =

     0     1     1     0     0
     0     1     1     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


ans =

     0     0     1     1     0
     0     0     1     1     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


ans =

     0     0     0     1     1
     0     0     0     1     1
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0


ans =

     0     0     0     0     0
     1     1     0     0     0
     1     1     0     0     0
     0     0     0     0     0
     0     0     0     0     0


ans =

     0     0     0     0     0
     0     1     1     0     0
     0     1     1     0     0
     0     0     0     0     0
     0     0     0     0     0


ans =

     0     0     0     0     0
     0     0     1     1     0
     0     0     1     1     0
     0     0     0     0     0
     0     0     0     0     0


ans =

     0     0     0     0     0
     0     0     0     1     1
     0     0     0     1     1
     0     0     0     0     0
     0     0     0     0     0


ans =

     0     0     0     0     0
     0     0     0     0     0
     1     1     0     0     0
     1     1     0     0     0
     0     0     0     0     0


ans =

     0     0     0     0     0
     0     0     0     0     0
     0     1     1     0     0
     0     1     1     0     0
     0     0     0     0     0


ans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     1     1     0
     0     0     1     1     0
     0     0     0     0     0


ans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     1     1
     0     0     0     1     1
     0     0     0     0     0


ans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     1     1     0     0     0
     1     1     0     0     0


ans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     1     1     0     0
     0     1     1     0     0


ans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     1     1     0
     0     0     1     1     0


ans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     1     1
     0     0     0     1     1
+5

bsxfun -

%// Get sizes and form size parameters for creating output
[mA,nA] = size(A);
[mB,nB] = size(B);
mC = mA - mB + 1;
nC = nA - nB + 1;

%// Get linear indices
stage1 = bsxfun(@plus,[1:mB]',[0:nB-1]*mA);    %//'
stage2 = bsxfun(@plus,[1:mC]',[0:nC-1]*mA)-1;  %//'
idx = bsxfun(@plus,stage1(:),stage2(:).' + [0:mC*nC-1]*mA*nA);   %//'

%// Replicate A to setup output; index into it with idx & replace B
C = repmat(A,1,1,mC*nC);
C(idx) = repmat(B(:),1,mC*nC)

-

A =
     1     1     8     4
     9     8     8     2
     7     9     5     1
     7     9     2     9
B =
     3     5
     3     6
     3     1
C(:,:,1) =
     3     5     8     4
     3     6     8     2
     3     1     5     1
     7     9     2     9
C(:,:,2) =
     1     1     8     4
     3     5     8     2
     3     6     5     1
     3     1     2     9
C(:,:,3) =
     1     3     5     4
     9     3     6     2
     7     3     1     1
     7     9     2     9
....

C(:,:,6) =
     1     1     8     4
     9     8     3     5
     7     9     3     6
     7     9     3     1
+3

I think a good way to do something like this

A = zeros(5);
B = ones(2);
C = cell(size(A,1)-size(B,1) + 1, size(A,2)-size(B,2) + 1);

for i = 1:size(A,1)-size(B,1) + 1
    for j = 1:size(A,2)-size(B,2) + 1
        C{i, j} = A;
        C{i, j}(i:i+size(B,1) - 1, j:j+size(B,2) - 1) = B;

        % Additional code here
    end
end

C = C(:);

% Additional code here
+2
source

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


All Articles