ee.Array.firstNonZero
On an element-wise basis, selects the first value if it is non-zero, and the second value otherwise.
Usage | Returns |
---|
Array.firstNonZero(right) | Array |
Argument | Type | Details |
---|
this: left | Array | The left-hand value. |
right | Array | The right-hand value. |
Examples
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]]));
Python setup
See the
Python Environment page for information on the Python API and using
geemap
for interactive development.
import ee
import geemap.core as geemap
empty = ee.Array([], ee.PixelType.int8())
display(empty.firstNonZero(empty)) # []
display(ee.Array([0]).firstNonZero(0)) # [0]
display(ee.Array([0]).firstNonZero([0])) # [0]
display(ee.Array([0]).firstNonZero([1])) # [1]
display(ee.Array([2]).firstNonZero([3])) # [2]
display(ee.Array([1]).firstNonZero([0])) # [1]
display(ee.Array([-1, 0, 1]).firstNonZero([2, -1, 2])) # [-1, -1, 1]
# [[1, 2], [3, 4]]
display(ee.Array([[1, 2], [0, 0]]).firstNonZero([[0, 0], [3, 4]]))
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[[["`Array.firstNonZero()` selects the first array's element if it's non-zero; otherwise, it selects the corresponding element from the second array."],["This function operates element-wise on two input arrays (`left` and `right`)."],["If the `left` array element is 0, the corresponding element from the `right` array is chosen."],["The function returns a new array containing the selected elements."],["It's useful for scenarios like conditional data selection based on zero/non-zero values."]]],["The `firstNonZero` method compares two arrays (`left` and `right`) element-wise. If an element in the `left` array is non-zero, that value is selected. Otherwise, the corresponding element from the `right` array is chosen. The method returns a new array where each element is the result of this comparison. Both JavaScript and Python examples show how to implement and the result. The return value is an array.\n"]]