Search code examples
matlabmatrixmove

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,


Solution

  • 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