0

I have a function which I am trying to vectorize. Inside the function I have the following code.

A = np.c_[xdata, ydata, np.ones(len(zdata))]

Where x_data, y_data, z_data are all 1x5 array, eg. [1,2,3,4,5]. The resulting output for A would be

array([[1.90155189, 1.64412979, 1.        ],
       [2.44148892, 1.73851717, 1.        ],
       [1.65259189, 2.10693759, 1.        ],
       [2.52045732, 2.30939049, 1.        ],
       [1.53516213, 2.39788003, 1.        ]])

I would like to convert this part of the function to work on an array of inputs (eg. 1000 rows of 5 columns) for x, y, z. I naively tried to just feed the arrays into this function with the following output for the first row.

array([1.90155189, 2.44148892, 1.65259189, 2.52045732, 1.53516213,
       1.64412979, 1.73851717, 2.10693759, 2.30939049, 2.39788003,
       1.        ])

Here is an example for the inputs for the first result:

x=[1.90155189 2.44148892 1.65259189 2.52045732 1.53516213]
y=[1.64412979 1.73851717 2.10693759 2.30939049 2.39788003]
z=[0.23273446 0.57301046 0.89755946 0.07169598 0.41394575]

Let's say now I have the following data for the second method:

x_array = [[1.90155189 2.44148892 1.65259189 2.52045732 1.53516213],
           [1.90155189 2.44148892 1.65259189 2.52045732 1.53516213],
           [1.90155189 2.44148892 1.65259189 2.52045732 1.53516213]]
y_array = [[1.64412979 1.73851717 2.10693759 2.30939049 2.39788003],
           [1.64412979 1.73851717 2.10693759 2.30939049 2.39788003],
           [1.64412979 1.73851717 2.10693759 2.30939049 2.39788003]]
z_array = [[0.23273446 0.57301046 0.89755946 0.07169598 0.41394575],
           [0.23273446 0.57301046 0.89755946 0.07169598 0.41394575],
           [0.23273446 0.57301046 0.89755946 0.07169598 0.41394575]]

With an expected output of

     [[[1.90155189, 1.64412979, 1.        ],
       [2.44148892, 1.73851717, 1.        ],
       [1.65259189, 2.10693759, 1.        ],
       [2.52045732, 2.30939049, 1.        ],
       [1.53516213, 2.39788003, 1.        ]],
       [[1.90155189, 1.64412979, 1.        ],
       [2.44148892, 1.73851717, 1.        ],
       [1.65259189, 2.10693759, 1.        ],
       [2.52045732, 2.30939049, 1.        ],
       [1.53516213, 2.39788003, 1.        ]],
      [[1.90155189, 1.64412979, 1.        ],
       [2.44148892, 1.73851717, 1.        ],
       [1.65259189, 2.10693759, 1.        ],
       [2.52045732, 2.30939049, 1.        ],
       [1.53516213, 2.39788003, 1.        ]]]
Cr1064
  • 409
  • 5
  • 15

1 Answers1

0

You can use this:

EDIT: Commented below line, added the next line, thanks to @hpaulj's comment.

# new_A = np.stack((x,y,np.ones_like(z)), axis=1).swapaxes(1,2)
new_A = np.stack((x,y,np.ones_like(z)), axis=2)

Testing it out:

THOUSAND = 6
x = np.random.randint(1,5,size=(THOUSAND,5))
y = np.random.randint(1,5,size=(THOUSAND,5))
z = np.random.randint(1,5,size=(THOUSAND,5))

print (x)
print (y)
print (z)

new_A = np.stack((x,y,np.ones_like(z)), axis=1).swapaxes(1,2)
print (new_A)

Output:

[[1 2 2 1 1]      # print(x)
 [4 4 4 4 4]
 [1 2 1 3 3]
 [2 3 1 4 4]
 [1 1 4 1 4]
 [4 1 3 3 2]]
[[2 2 3 4 4]      # print(y)
 [1 1 4 2 1]
 [3 3 1 1 2]
 [1 1 2 1 3]
 [3 2 1 4 3]
 [4 4 1 3 2]]
[[3 4 3 2 2]      # print(z)
 [4 2 4 3 3]
 [3 3 4 1 4]
 [4 3 3 3 1]
 [4 1 1 3 3]
 [4 1 4 3 3]]

# new_A output

[[[1 2 1]      # print(new_A)
  [2 2 1]
  [2 3 1]
  [1 4 1]
  [1 4 1]]

 [[4 1 1]
  [4 1 1]
  [4 4 1]
  [4 2 1]
  [4 1 1]]

 [[1 3 1]
  [2 3 1]
  [1 1 1]
  [3 1 1]
  [3 2 1]]

 [[2 1 1]
  [3 1 1]
  [1 2 1]
  [4 1 1]
  [4 3 1]]

 [[1 3 1]
  [1 2 1]
  [4 1 1]
  [1 4 1]
  [4 3 1]]

 [[4 4 1]
  [1 4 1]
  [3 1 1]
  [3 3 1]
  [2 2 1]]]
fountainhead
  • 3,584
  • 1
  • 8
  • 17
  • 1
    With a change in `axis` you don't need the `swapaxes`: `np.stack((x,y,np.ones_like(z)), axis=2)` – hpaulj Mar 27 '19 at 16:49