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:
Use with AIimport 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:
Use with AIarray([[ 1, 4, 6],
[ 1, 5, 7],
[ 2, 4, 8],
[ 2, 5, 9],
[ 3, 4, 10],
[ 3, 5, 11]])
Answer from kuzand on Stack OverflowUsing 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:
Use with AIimport 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:
Use with AIarray([[ 1, 4, 6],
[ 1, 5, 7],
[ 2, 4, 8],
[ 2, 5, 9],
[ 3, 4, 10],
[ 3, 5, 11]])
For a pure Python solution, to work with any list size.
Use with AIx = [11,12,13,14,15]
y = [21,23,24]
z = [31,32,33,34,35,36,37]
This verbose code:
Use with AItmp = []
for i in range(max(len(x), len(y), len(z))):
for yy in y:
tmp.append([x[i%len(x)], yy])
for i in range(len(tmp)):
tmp[i].append((z[i%len(z)]))
Returns
Use with AI# [[11, 21, 31],
# [11, 23, 32],
# [11, 24, 33],
# [12, 21, 34],
# [12, 23, 35],
# [12, 24, 36],
# [13, 21, 37],
# [13, 23, 31],
# [13, 24, 32],
# [14, 21, 33],
# [14, 23, 34],
# [14, 24, 35],
# [15, 21, 36],
# [15, 23, 37],
# [15, 24, 31],
# [11, 21, 32],
# [11, 23, 33],
# [11, 24, 34],
# [12, 21, 35],
# [12, 23, 36],
# [12, 24, 37]]
np.put places values from b into a at the target indices, ind. If v is shorter than ind, its values are repeated as necessary:
import numpy as np
a = np.empty(100)
b = np.arange(1, 4, 0.25)
ind = np.arange(len(a))
np.put(a, ind, b)
print(a)
yields
[ 1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
1. 1.25 1.5 1.75 2. 2.25 2.5 2.75 3. 3.25 3.5 3.75
1. 1.25 1.5 1.75]
updating solution to fit description
a = np.empty(100)
filler = np.arange(1,4,0.25)
index = np.arange(a.size)
np.put(a,index,filler)
Hello everyone,
I haven't done any type of linear algebra work with python before, only matlab. In matlab you create a matrix with the proper dimensions, then start filling it up with vectors of the appropriate size. I've tried to look up how to do this in python using a loop, and it doesn't seem as straightforward. It seems you first need to create a list, and append that list. Then convert that list to a numpy array. And then from there it appears you stack them? But stacking seems to only stack 1D and 2D arrays, so it's not exactly what I'm looking for.
For example:
import numpy as np
matrix=np.zeros((rows,columns))
list=[]
for i in range(columns):
for x in range(rows):
list.append(x)
arr=np.array(list)
matrix.append(arr)
list.clear() So in the above case, I've made a list, and converted it into an array with shape (rows, 1). Then I want to add it to the first column of my matrix. Then through the loop, I start to fill my matrix up by filling it one by one with these vectors. However, the issue is I'm trying to add a 1D array, to a multi-D matrix (which numpy doesn't allow you to do).
Hpw do I fill up a matrix using numpy?