1
import numpy as np

a = np.array([0.75, 0.5, 0.21])
one_list = [1] * 3
L_vec = np.diag(one_list)
L_vec[1,0] = a[0]
print(L_vec)

Expected Result:

[[1,0,0],[0.75,1,0],[0,0,1]]

Actual Result:

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

this is the result I got. I have no idea why.

1 Answer 1

1

By default dtype for np.diag is int

convert it into float so your float values from array a can replace older value

L_vec = L_vec.astype(float)

Use below code

a = np.array([0.75, 0.5, 0.21])
one_list = [1]*3
L_vec = np.diag(one_list)
L_vec = L_vec.astype(float)

L_vec[1,0] = a[0]
print(L_vec)

Output:

[[1.   0.   0.  ]
 [0.75 1.   0.  ]
 [0.   0.   1.  ]]

You can check datatype using print(L_vec.dtype)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.