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 ?

0

2 Answers 2

3

You can do it using Kronecker tensor product:

B = kron(A,ones(n)); 
Sign up to request clarification or add additional context in comments.

1 Comment

kron is the best. I would add B=kron(ones(n),A) for the second question.
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 

2 Comments

Additional question: If I want to duplicate like following 2 3 2 3 <br/> 5 6 5 6 <br/> 2 3 2 3 <br/> 5 6 5 6 <br/> How this can be archived ?
@Aditya That's easier: repmat(M, v, h)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.