Issue
I want to fill an array with some number, the output should be like this:
[[x1, y1, z1],
[x1, y2, z2],
[x2, y1, z3],
[x2, y2, z4],
[x3, y1, z5],
[x3, y2, z6]]
I have the data x, y, and z (lists) with 3 values x, 2 values of y and 6 values of z inside the data.
I tried to create a new array and then fill it with for or while loop, but I can't get it fully filled.
How can I do it in numpy with python without filling it manually?
Solution
Using np.repeat and np.tile on x and y respectively to get the desired columns and then stacking them together with z into one array:
import numpy as np
x = [1, 2, 3]
y = [4, 5]
z = [6, 7, 8, 9, 10, 11]
a = np.array([np.repeat(x, 2), np.tile(y, 3), z]).T
result:
array([[ 1, 4, 6],
[ 1, 5, 7],
[ 2, 4, 8],
[ 2, 5, 9],
[ 3, 4, 10],
[ 3, 5, 11]])
Answered By - Andreas K.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.