[Python] Is there a simpler way to select 2D vectors bounded by a box from a NumPy array?

Discussão em 'Python' iniciado por Stack, Julho 19, 2025.

  1. Stack

    Stack Membro Participativo

    This Python script:

    import numpy as np

    a = np.arange(12).reshape(6, 2)
    inf = np.array([2, 2])
    sup = np.array([9, 9])
    b = (inf < a) & (a < sup)
    r = a[b[:, 0] & b[:, 1]]


    creates a subarray r:

    [[4 5]
    [6 7]]


    of array a:

    [[ 0 1]
    [ 2 3]
    [ 4 5]
    [ 6 7]
    [ 8 9]
    [10 11]]


    containing only 2D vectors, which fit inside the box with lower left corner at [2, 2] and upper right corner at [9, 9]. I suspect there is a simpler way to compute it. Any suggestions?

    Continue reading...

Compartilhe esta Página