bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / ma / core / diag
function
numpy.ma.core:diag
source: build-install/usr/lib/python3.14/site-packages/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_xIsolate 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