Interleave rows of two numpy arrays in Python

后端 未结 3 1782
鱼传尺愫
鱼传尺愫 2021-01-17 09:51

I wanted to interleave the rows of two numpy arrays of the same size. I came up with this solution.

# A and B are same-shaped arrays
A = numpy.ones((4,3))
B         


        
3条回答
  •  Happy的楠姐
    2021-01-17 10:23

    It is maybe a bit clearer to do:

    A = np.ones((4,3))
    B = np.zeros_like(A)
    
    C = np.empty((A.shape[0]+B.shape[0],A.shape[1]))
    
    C[::2,:] = A
    C[1::2,:] = B
    

    and it's probably a bit faster as well, I'm guessing.

提交回复
热议问题