3

What I am trying to achieve is that I have a matrix like that:

axy
axy
axy
axy

And I want to expand that matrix so that it will become:

aaaxxxyyy
aaaxxxyyy
aaaxxxyyy
aaaxxxyyy

Is there a function that I can use to manage this transformation easily? I would like to use a better way than tiling each column separately and appending them back.

Thanks in advance.

2
  • Do you want to alter the matrix or just print it that way? Commented Aug 5, 2015 at 15:15
  • Just for some matrix operations with another one, I don't further need that expanded version. Commented Aug 6, 2015 at 6:42

1 Answer 1

7

You can use np.repeat:

>>> import numpy as np
>>> a = np.arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> b = np.repeat(a, 3, axis=1) # array, times, axis
>>> b
array([[0, 0, 0, 1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4, 5, 5, 5],
       [6, 6, 6, 7, 7, 7, 8, 8, 8]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.