Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Repeat values of an array on both the axes

Say I have this array:

array = np.array([[1,2,3],[4,5,6],[7,8,9]])
returns:
123
456
789

How should I go about getting it to do something like this

returns:
111222333
111222333
111222333
444555666
444555666
444555666
777888999
777888999
777888999

I don’t know what to google to get an answer as all of my results have been different issues since I imagine this isn’t too common of a thing
Anyway, any and all help would be greatly appreciated 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’d have to use np.repeat twice here.

np.repeat(np.repeat(array, 3, axis=1), 3, axis=0)

# [[1 1 1 2 2 2 3 3 3]
#  [1 1 1 2 2 2 3 3 3]
#  [1 1 1 2 2 2 3 3 3]
#  [4 4 4 5 5 5 6 6 6]
#  [4 4 4 5 5 5 6 6 6]
#  [4 4 4 5 5 5 6 6 6]
#  [7 7 7 8 8 8 9 9 9]
#  [7 7 7 8 8 8 9 9 9]
#  [7 7 7 8 8 8 9 9 9]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading