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

anubhav picture anubhav · Oct 29, 2009 · Viewed 9k times · Source

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?

Answer

gnovice picture gnovice · Oct 29, 2009

In newer versions of MATLAB (R2015a and later) the easiest way to do this is using the repelem function:

B = repelem(A, 2, 2);

For older versions, a short alternative to the other (largely) indexing-based solutions is to use the functions kron and ones:

>> A = [2 6; 8 4];
>> B = kron(A, ones(2))

B =

     2     2     6     6
     2     2     6     6
     8     8     4     4
     8     8     4     4