matlabmatrixrepeattiling

How do I convert a 2X2 matrix to 4X4 matrix in MATLAB?


I need some help in converting a 2X2 matrix to a 4X4 matrix in the following manner:

A = [2 6;
     8 4]

should become:

B = [2 2 6 6;
     2 2 6 6;
     8 8 4 4;
     8 8 4 4]

How would I do this?


Solution

  • A = [2 6; 8 4];
    % arbitrary 2x2 input matrix
    
    B = repmat(A,2,2);
    % replicates rows & columns but not in the way you want
    
    B = B([1 3 2 4], :);
    % swaps rows 2 and 3
    
    B = B(:, [1 3 2 4]);
    % swaps columns 2 and 3, and you're done!