ee.Array.firstNonZero

Stay organized with collections Save and categorize content based on your preferences.
On an element-wise basis, selects the first value if it is non-zero, and the second value otherwise.

UsageReturns
Array.firstNonZero(right)Array
ArgumentTypeDetails
this: leftArrayThe left-hand value.
rightArrayThe right-hand value.

Examples

Code Editor (JavaScript)

var empty = ee.Array([], ee.PixelType.int8());
print(empty.firstNonZero(empty));  // []

print(ee.Array([0]).firstNonZero(0));  // [0]
print(ee.Array([0]).firstNonZero([0]));  // [0]
print(ee.Array([0]).firstNonZero([1]));  // [1]
print(ee.Array([2]).firstNonZero([3]));  // [2]
print(ee.Array([1]).firstNonZero([0]));  // [1]

print(ee.Array([-1, 0, 1]).firstNonZero([2, -1, 2]));  // [-1,-1,1]

// [[1,2],[3,4]]
print(ee.Array([[1, 2], [0, 0]]).firstNonZero([[0, 0], [3, 4]]));

Colab (Python)

empty = ee.Array([], ee.PixelType.int8())
print(empty.firstNonZero(empty).getInfo())  # []

print(ee.Array([0]).firstNonZero(0).getInfo())  # [0]
print(ee.Array([0]).firstNonZero([0]).getInfo())  # [0]
print(ee.Array([0]).firstNonZero([1]).getInfo())  # [1]
print(ee.Array([2]).firstNonZero([3]).getInfo())  # [2]
print(ee.Array([1]).firstNonZero([0]).getInfo())  # [1]

print(ee.Array([-1, 0, 1]).firstNonZero([2, -1, 2]).getInfo())  # [-1, -1, 1]

# [[1, 2], [3, 4]]
print(ee.Array([[1, 2], [0, 0]]).firstNonZero([[0, 0], [3, 4]]).getInfo())