Sourav kumar Singh
Sourav kumar Singh

Reputation: 5

How Can I update a particular row in numpy matrix?

Here I am trying to update a specific row of my numpy matrix but it is acting a bit weird

tpm1=np.array([[1,0, 0, 0, 0],[1, 0, 0, 0, 0],[1,0, 0, 0, 0],[1,0, 0, 0, 0],[1,0, 0, 0, 0]])
tpm1[1,:]=0.15*tpm1[1,:]
[[1 0 0 0 0]
 [1 0 0 0 0]
 [1 0 0 0 0]
 [1 0 0 0 0]
 [1 0 0 0 0]]

Output is given as

[[1 0 0 0 0]
 [0 0 0 0 0]
 [1 0 0 0 0]
 [1 0 0 0 0]
 [1 0 0 0 0]]

But I expect [0.15,0,0,0,0] in the second row. I am not sure what's happening here

Upvotes: 0

Views: 97

Answers (1)

AJH
AJH

Reputation: 799

Just declare the dtype as float when making your array:

tpm1=np.array([[1, 0, 0, 0, 0],[1, 0, 0, 0, 0],[1,0, 0, 0, 0],[1,0, 0, 0, 0],[1,0, 0, 0, 0]], dtype=float)

Alternatively, you can make one of the values 1.0 or 0.0 when declaring the array, e.g.:

tpm1=np.array([[1.0, 0, 0, 0, 0],[1, 0, 0, 0, 0],[1,0, 0, 0, 0],[1,0, 0, 0, 0],[1,0, 0, 0, 0]])

This will automatically set the array to dtype float.

Upvotes: 1

Related Questions