Search code examples
numpypython-3.6

Replace all off diagonal elements in numpy array


I have below numpy array

import numpy as np
np.identity(13)

Now I like to replace all off-diagonal elements with some other number, say 0.45.

Is there any direct method available to perform this?


Solution

  • You can use numpy.where

    np.where(np.identity(13)==0, 0.45, 1)