Trouble saving array to columns

Viewed 43

I have the following code:

dvbydt = np.full([3,N],0)
dvbydt[:,0] = np.array([-1.47813483e-03, -2.23682946e-03, -1.06122312e-05])

I was under the impression that this would save the whole of the first column as the array with three values on the right hand side but it still returns an array with all 0's. What am I doing wrong? No errors come up, and they have the same shape but nothing seems to save into the dvbydt array. Any help is much appreciated

1 Answers

If you check the type of the entries, they are integers:

type(dvbydt[0][0])
# numpy.int32

So it does work, it just approximates them to the nearest integer which is 0.

When you define the array, use 0.0 instead than 0:

N = 3
dvbydt = np.full([3,N],0.0)
dvbydt[:,0] = np.array([-1.47813483e-03, -2.23682946e-03, -1.06122312e-05])

dvbydt
# array([[-1.47813483e-03,  0.00000000e+00,  0.00000000e+00],
#       [-2.23682946e-03,  0.00000000e+00,  0.00000000e+00],
#       [-1.06122312e-05,  0.00000000e+00,  0.00000000e+00]])

Alternatively, specify the dtype in the array arguments:

N = 3
dvbydt = np.full([3,N],0, dtype=float)
dvbydt[:,0] = np.array([-1.47813483e-03, -2.23682946e-03, -1.06122312e-05])

dvbydt
# array([[-1.47813483e-03,  0.00000000e+00,  0.00000000e+00],
#       [-2.23682946e-03,  0.00000000e+00,  0.00000000e+00],
#       [-1.06122312e-05,  0.00000000e+00,  0.00000000e+00]])
Related