Skip to content
Advertisement

np.diag not including 0s in the array

I have an expression that yields the following result:

JavaScript

I want to make a diagonal 2X2 matrix which has 0.5 and 0 on its diagonal. But when I use the following code:

JavaScript

A being the above array, I get the following result:

JavaScript

Why does python not include the second element from A on the array and how can I include it?

Advertisement

Answer

x is 2d:

JavaScript

Read diag docs – given a 2d array, it returns the diagonal:

JavaScript

Given a 1d array it returns a 2d array:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement