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]
Answer from unutbu on Stack Overflownp.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)
Filling an array in python - Stack Overflow
python - How to fill a list - Stack Overflow
How to fill a matrix using numpy
Python - Filling an array with calculated values - Stack Overflow
How do I initialize an array in Python with a size?
How do I initialize a 2D NumPy array with zeros?
Does np.empty() create an array of zeros?
arr = np.zeros(10)
arr[4:7] = 1
print(arr)
Output:
array([0., 0., 0., 0., 1., 1., 1., 0., 0., 0.])
For computed ranges slices are quite useful:
ar = np.zeros(3*6).reshape((3,6))
for i in range(3):
ar[i, slice(i+1,i+3)] = 1
print(ar)
Output:
[[0. 1. 1. 0. 0. 0.]
[0. 0. 1. 1. 0. 0.]
[0. 0. 0. 1. 1. 0.]]
Also, these allow specification of stride (step) values:
ar = np.zeros(2*10).reshape((2,10))
for i in range(2):
ar[i, slice(i+1, i+8, 2)] = 2-i
print(ar)
Output:
[[0. 2. 0. 2. 0. 2. 0. 2. 0. 0.]
[0. 0. 1. 0. 1. 0. 1. 0. 1. 0.]]
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]])
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]]
Consider the usage of extend:
>>> l = [] >>> l.extend(range(1, 6)) >>> print l [1, 2, 3, 4, 5] >>> l.extend(range(1, 6)) >>> print l [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
If you want to make a function (doing the same):
def fillmylist(l, n):
l.extend(range(1, n + 1))
l = []
fillmylist(l, 5)
A function without an explicit return or yield returns None. What you want is
def fill_list(l, n):
for i in xrange(1, n+1):
l.append(i)
return l
but that's very unpythonic. You'd better just call range(1, n+1) which also returns the list [1,2,3,4,5] for n=5:
def fill_list(n):
return range(1, n+1)
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?