简体   繁体   中英

How can I create the following 4*4 Matrix from a 2*2 Matrix in Python?

a = np.array([[1,2],[3,4]])
b = np.array([[1,2,1,2],[3,4,3,4],[1,2,1,2],[3,4,3,4]])

I want to convert a to b here. How can I do this?

Thanks in advance

For those curious

Even though OP figured this out already: Here's how this can be achieve very easily

a = np.array([[1, 2], [3, 4]])
np.tile(a, (2, 2))  # (2, 2) = extend columns by 2 and rows by 2
>>> array([[1, 2, 1, 2],
           [3, 4, 3, 4],
           [1, 2, 1, 2],
           [3, 4, 3, 4]])

np.tile(a, 2)  # 2 = extend columns by 2
>>> array([[1, 2, 1, 2],
           [3, 4, 3, 4]])

I wanted to comment on Bobs Burgers post, but don't have enough reputation. I was playing with np.repeat, and that wasn't quite getting me there. The closest I achieved was:

>>> import numpy as np
>>> a = np.array([[1,2],[3,4]])
array([[1, 2],
       [3, 4]])
>>> a.repeat(2, axis=0).repeat(2, axis=1)
array([[1, 1, 2, 2],
       [1, 1, 2, 2],
       [3, 3, 4, 4],
       [3, 3, 4, 4]])

I like the tile answer, but this might be useful for people looking for something slightly different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM