pythonarraysmatrix

Concatenate a few row vectors into a matrix


Four 1X3 row vectors, trying to concatenate them into a 4X3 martrix. Here is my code that is not working:

ul = np.array([-320, 240, 1])
    ur = np.array([320, 240, 1])
    br = np.array([320, -240, 1])
    bl = np.array([-320, -240, 1])
    
    corners =np.concatenate( (ul,ur, br, bl), axis=1)

Desired output:

-320, 240, 1

320, 240, 1

320, -240, 1

-320, -240, 1

Thanks.


Solution

  • You can do something like this:

    import numpy as np
    
    ul = np.array([-320, 240, 1])
    ur = np.array([320, 240, 1])
    br = np.array([320, -240, 1])
    bl = np.array([-320, -240, 1])
    
    corners = np.array([ul, ur, br, bl]) ### or np.stack([ul, ur, br, bl], axis=0)
    
    print(corners)
    

    Output:

    [[-320  240    1]
     [ 320  240    1]
     [ 320 -240    1]
     [-320 -240    1]]