user12587364
user12587364

Reputation:

Test that the last nth rows of an array are all zeros

I have this array:

A = np.array([[[ 1.8, -3.1, -3.5,  2.2],
        [ 1.5, -6.6,  1.1,  1.1],
        [ 8.9,  4.8, -1.2,  3.6],
        [ 1.3, -7.4,  7.4,  1. ],
        [ 6.3,  0. ,  0. ,  3. ],
        [ 6.3,  0. , -6.3,  0. ],
        [ 6.3, -6.3,  6.3,  3.3],
        [ 0. ,  0. ,  0. ,  0. ],
        [ 0. ,  0. ,  0. ,  0. ],
        [ 0. ,  0. ,  0. ,  0. ]]])

So I want check if the last 3-rows of this array are all zero:

counter = 0
if A[last-3-rows==0]:
    counter += 1

Upvotes: 0

Views: 19

Answers (1)

sacuL
sacuL

Reputation: 51335

You can use np.all to check the last three rows with this index:

>>> np.all(A[:, -3:] == 0)
# or alternatively
# >>> (A[:, -3:] == 0).all()
True

If you want it as an integer:

>>> np.all(A[:, -3:] == 0).astype(int)
1

Upvotes: 1

Related Questions