Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 923
anubhav Avatar asked Nov 26 '22 21:11

anubhav


2 Answers

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
like image 111
gnovice Avatar answered Dec 15 '22 10:12

gnovice


Can be done even easier than Jason's solution:

B = A([1 1 2 2], :);  % replicate the rows
B = B(:, [1 1 2 2]);  % replicate the columns
like image 39
Martijn Avatar answered Dec 15 '22 10:12

Martijn