Fill A Column Of A Numpy Array With Another Array
I have: x = np.zeros((96,11,11,2,10),dtype=np.float64) y = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype=np.float64) x[:,:,:,0,0] = y print x[0,:,:,0,0] i get: [[ 0. 10.
Solution 1:
You need to change y
from 1D to 2D (with one column):
x[:,:,:,0,0] = y[:, np.newaxis]
or,
x[:,:,:,0,0] = y.reshape(11,1)
Solution 2:
If you want the output to be the transpose, just do:
>>> import numpy as np
>>> x = np.zeros((96,11,11,2,10),dtype=np.float64)
>>> y = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype=np.float64)
>>> for i in range(x.shape[0]):
>>> x[i,:,:,0,0] = x[i,:,:,0,0].T
>>> print x[0,:,:,0,0]
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 10. 10. 10. 10. 10. 10. 10. 10. 10. 10. 10.]
[ 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]
[ 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30.]
[ 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40.]
[ 50. 50. 50. 50. 50. 50. 50. 50. 50. 50. 50.]
[ 60. 60. 60. 60. 60. 60. 60. 60. 60. 60. 60.]
[ 70. 70. 70. 70. 70. 70. 70. 70. 70. 70. 70.]
[ 80. 80. 80. 80. 80. 80. 80. 80. 80. 80. 80.]
[ 90. 90. 90. 90. 90. 90. 90. 90. 90. 90. 90.]
[ 100. 100. 100. 100. 100. 100. 100. 100. 100. 100. 100.]]
It updates the first dimension, this is the output for 34th index:
>>> print x[34,:,:,0,0]
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 10. 10. 10. 10. 10. 10. 10. 10. 10. 10. 10.]
[ 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]
[ 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30.]
[ 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40.]
[ 50. 50. 50. 50. 50. 50. 50. 50. 50. 50. 50.]
[ 60. 60. 60. 60. 60. 60. 60. 60. 60. 60. 60.]
[ 70. 70. 70. 70. 70. 70. 70. 70. 70. 70. 70.]
[ 80. 80. 80. 80. 80. 80. 80. 80. 80. 80. 80.]
[ 90. 90. 90. 90. 90. 90. 90. 90. 90. 90. 90.]
[ 100. 100. 100. 100. 100. 100. 100. 100. 100. 100. 100.]]
Solution 3:
The problem is simple: you're using a row vector for y
instead of a column vector, so it's filling by row instead of by column.
More technically, you've got an array of shape (11,)
, instead of an array of (11, 1)
, so it broadcasts to (1, 11)
when filling a 2D array.
Compare:
>>> x = np.zeros((96,11,11,2,10),dtype=np.float64)
>>> y = np.array([[0],[10],[20],[30],[40],[50],[60],[70],[80],[90],[100]],dtype=np.float64)
>>> x[:,:,:,0,0]=y
>>> print x[0,:,:,0,0]
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 10. 10. 10. 10. 10. 10. 10. 10. 10. 10. 10.]
[ 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]
[ 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30.]
[ 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40.]
[ 50. 50. 50. 50. 50. 50. 50. 50. 50. 50. 50.]
[ 60. 60. 60. 60. 60. 60. 60. 60. 60. 60. 60.]
[ 70. 70. 70. 70. 70. 70. 70. 70. 70. 70. 70.]
[ 80. 80. 80. 80. 80. 80. 80. 80. 80. 80. 80.]
[ 90. 90. 90. 90. 90. 90. 90. 90. 90. 90. 90.]
[ 100. 100. 100. 100. 100. 100. 100. 100. 100. 100. 100.]]
Of course in your real code, y
probably isn't a literal, but a result of some earlier computation. (And even if it is a literal, you don't want to type all those extra brackets.) So, assume y
is inherently a row vector, as we have to deal with it.
So, just reshape it on the fly:
>>> x = np.zeros((96,11,11,2,10),dtype=np.float64)
>>> y = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype=np.float64)
>>> x[:,:,:,0,0] = y.reshape((11, 1))
Same result.
Post a Comment for "Fill A Column Of A Numpy Array With Another Array"