0

I have a square matrix say n by n; I want to expand this matrix to n^2 by n^2 such that values in the position are repeated. e.g A matrix is

2 3
5 6

I want to generate matrix B such that

2 2 3 3
2 2 3 3
5 5 6 6
5 5 6 6

How can this be done in matlab? And need to be generalized for any square matrix

Additional question: If I want to duplicate like following
2 3 2 3
5 6 5 6
2 3 2 3
5 6 5 6

How this can be archived ?

Aditya
  • 11
  • 2

2 Answers2

3

You can do it using Kronecker tensor product:

B = kron(A,ones(n));
OmG
  • 18,337
  • 10
  • 57
  • 90
1

Let the data be

M = [2 3; 5 6];   % initial matrix
v = 2;            % vertical repetition factor 
h = 3;            % horizontal repetition factor

In addition to using kron as shown by @Omg's answer, you can do it using just indexing:

result = M(ceil(1/v:1/v:end), ceil(1/h:1/h:end));

Or, in recent Matlab versions, you can use repelem:

result = repelem(M, v, h);

Either of the above gives

result =
     2     2     2     3     3     3
     2     2     2     3     3     3
     5     5     5     6     6     6
     5     5     5     6     6     6
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147