{ } Raw JSON

bundles / numpy 2.4.4 / numpy / ma / core / diag

function

numpy.ma.core:diag

source: /numpy/ma/core.py :7358

Signature

def   diag ( v k = 0 )

Summary

Extract a diagonal or construct a diagonal array.

Extended Summary

This function is the equivalent of numpy.diag that takes masked values into account, see numpy.diag for details.

Examples

import numpy as np
Create an array with negative values masked:
import numpy as np
x = np.array([[11.2, -3.973, 18], [0.801, -1.41, 12], [7, 33, -12]])
masked_x = np.ma.masked_array(x, mask=x < 0)
masked_x
Isolate the main diagonal from the masked array:
np.ma.diag(masked_x)
Isolate the first diagonal below the main diagonal:
np.ma.diag(masked_x, -1)

See also

numpy.diag

Equivalent function for ndarrays.

Aliases

  • numpy.ma.diag