Extract a diagonal or construct a diagonal array. This function is the equivalent of `numpy.diag` that takes masked values into account, see `numpy.diag` for details. See Also -------- numpy.diag : Equivalent function for ndarrays. Examples -------- Create an
(v, k=0)
| 7153 | |
| 7154 | |
| 7155 | def diag(v, k=0): |
| 7156 | """ |
| 7157 | Extract a diagonal or construct a diagonal array. |
| 7158 | |
| 7159 | This function is the equivalent of `numpy.diag` that takes masked |
| 7160 | values into account, see `numpy.diag` for details. |
| 7161 | |
| 7162 | See Also |
| 7163 | -------- |
| 7164 | numpy.diag : Equivalent function for ndarrays. |
| 7165 | |
| 7166 | Examples |
| 7167 | -------- |
| 7168 | |
| 7169 | Create an array with negative values masked: |
| 7170 | |
| 7171 | >>> import numpy as np |
| 7172 | >>> x = np.array([[11.2, -3.973, 18], [0.801, -1.41, 12], [7, 33, -12]]) |
| 7173 | >>> masked_x = np.ma.masked_array(x, mask=x < 0) |
| 7174 | >>> masked_x |
| 7175 | masked_array( |
| 7176 | data=[[11.2, --, 18.0], |
| 7177 | [0.801, --, 12.0], |
| 7178 | [7.0, 33.0, --]], |
| 7179 | mask=[[False, True, False], |
| 7180 | [False, True, False], |
| 7181 | [False, False, True]], |
| 7182 | fill_value=1e+20) |
| 7183 | |
| 7184 | Isolate the main diagonal from the masked array: |
| 7185 | |
| 7186 | >>> np.ma.diag(masked_x) |
| 7187 | masked_array(data=[11.2, --, --], |
| 7188 | mask=[False, True, True], |
| 7189 | fill_value=1e+20) |
| 7190 | |
| 7191 | Isolate the first diagonal below the main diagonal: |
| 7192 | |
| 7193 | >>> np.ma.diag(masked_x, -1) |
| 7194 | masked_array(data=[0.801, 33.0], |
| 7195 | mask=[False, False], |
| 7196 | fill_value=1e+20) |
| 7197 | |
| 7198 | """ |
| 7199 | output = np.diag(v, k).view(MaskedArray) |
| 7200 | if getmask(v) is not nomask: |
| 7201 | output._mask = np.diag(v._mask, k) |
| 7202 | return output |
| 7203 | |
| 7204 | |
| 7205 | def left_shift(a, n): |