Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a small matrix inside of a bigger matrix in MATLAB

Let's assume that 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 ones (2x2):

>> B = ones(2)

B =

     1     1
     1     1

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

Which 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 C16 is equal 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, it is like smaller matrix (B) in moving inside of the bigger one (A).

Many thanks,

like image 946
Iman Avatar asked Dec 24 '22 16:12

Iman


2 Answers

You could achieve what you want using circshift(...) on the appropriate rows and columns to shift the values about the Matrix. The example you mention is the example shown in the 'Move Matrix Elements' portion of the page with a 4x4 matrix.

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

From the Mathworks website, there is a builtin function that seems like it would do exactly what you want. The exact code to show the 16 combinations on a 5x5 matrix that keep the illusion of a small matrix moving through a big one would be

EDITED: so it now has a 5x5x16 matrix with the outputs called 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

Which yields the output(NOTE output not edited)

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
like image 105
Matt Avatar answered Jan 05 '23 16:01

Matt


One vectorized approach with 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)

Sample run -

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
like image 27
Divakar Avatar answered Jan 05 '23 16:01

Divakar