numpy.ma.where#

ma.where(condition, x=<no value>, y=<no value>)[source]#

根據條件,傳回一個包含來自 xy 元素的遮罩陣列。

注意

當僅提供 condition 時,此函數與 nonzero 相同。本文件其餘部分僅涵蓋提供所有三個參數的情況。

參數:
conditionarray_like, bool

若為 True,則產生 x,否則產生 y

x, yarray_like, optional

要選擇的值。 xycondition 需要可廣播到相同的形狀。

傳回值:
outMaskedArray

一個遮罩陣列,其中條件為遮罩處的元素為 maskedcondition 為 True 處的元素來自 x,其他地方的元素來自 y

另請參閱

numpy.where

頂層 NumPy 模組中的等效函數。

nonzero

當省略 x 和 y 時呼叫的函數

範例

>>> import numpy as np
>>> x = np.ma.array(np.arange(9.).reshape(3, 3), mask=[[0, 1, 0],
...                                                    [1, 0, 1],
...                                                    [0, 1, 0]])
>>> x
masked_array(
  data=[[0.0, --, 2.0],
        [--, 4.0, --],
        [6.0, --, 8.0]],
  mask=[[False,  True, False],
        [ True, False,  True],
        [False,  True, False]],
  fill_value=1e+20)
>>> np.ma.where(x > 5, x, -3.1416)
masked_array(
  data=[[-3.1416, --, -3.1416],
        [--, -3.1416, --],
        [6.0, --, 8.0]],
  mask=[[False,  True, False],
        [ True, False,  True],
        [False,  True, False]],
  fill_value=1e+20)