I have an issue with setting a value in an empty numpy 3D Matrix here is a simplified example of the code that I am using
import numpy as np
QMatrix = np.empty(((3,3,3))) # I do not want to use np.zero for a specific reason
QMatrix[0][0][0] = 57
print ("Position Values is " , Qmatrix[0][0][0])
This should set the value 57 @ [0][0][0] position of the matrix but when I print the matrix all I am getting is
Position Value is 5.70000000e+001
But what I want is that the value to be at as 57
any help would be appriciated. Thanks in advance
you must edit second line from
QMatrix = np.empty(((3,3,3)))
to
QMatrix = np.empty(((3,3,3)), dtype=int)
your script will be :
import numpy as np
QMatrix = np.empty(((3,3,3)), dtype=int) #QMatrix = np.empty(((3,3,3)))
QMatrix[0][0][0] = 57
The default dtype of numpy array is float64
. You can verify this in your code by
import numpy as np
QMatrix = np.empty(((3,3,3)))
print QMatrix.dtype
Outputs - dtype('float64')
So, If you want the array to be of integer type, you need to set dtype
explicitly while declaring numpy array
QMatrix = np.empty(((3,3,3)),dtype='int')
print QMatrix.dtype
Outputs - dtype('int64')
Now, if you try to assign first element of 3-D array ie QMatrix[0][0][0] = 57
it will be stored as int
type not float
.
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.