Question Details

No question body available.

Tags

python numpy logical-operators

Answers (3)

April 2, 2026 Score: 2 Rep: 577 Quality: Low Completeness: 80%

You could use multiple choices here:

  • & works for scalars.

  • np.logicaland.reduce()

  • np.all() with a list []

here you go the code comparing between the above methods in term of time:

# Source - https://stackoverflow.com/a/79919211

Posted by Mohammad Othman

Retrieved 2026-04-02, License - CC BY-SA 4.0

import numpy as np import time

N, M, K = 10000, 10000, 1000000 a = np.random.randint(2 * K, size=(N, M)) element = 9999

def method
and(): return ( np.any(a[0, :] == element) and np.any(a[-1, :] == element) and np.any(a[:, 0] == element) and np.any(a[:, -1] == element) )

def methodbitwise(): return ( np.any(a[0, :] == element) & np.any(a[-1, :] == element) & np.any(a[:, 0] == element) & np.any(a[:, -1] == element) )

def method
logicalreduce(): return np.logicaland.reduce( [ np.any(a[0, :] == element), np.any(a[-1, :] == element), np.any(a[:, 0] == element), np.any(a[:, -1] == element), ] )

def methodnpall(): return np.all( [ np.any(a[0, :] == element), np.any(a[-1, :] == element), np.any(a[:, 0] == element), np.any(a[:, -1] == element), ] )

iterations = 10000 methods = [ ('Python "and"', methodand), ('Bitwise "&"', methodbitwise), ("np.logicaland.reduce()", methodlogicalreduce), ("np.all() with list", methodnpall), ] for name, func in methods: start = time.perfcounter() for in range(iterations): result = func() elapsed = time.perfcounter() - start print(f"{name:
April 3, 2026 Score: 2 Rep: 3,654 Quality: Low Completeness: 50%

I don't think you can replace all of the ands with numpy operations, but you can replace two of them:

(a[::N-1, :] == K).any(axis=1).all() and (a[:, ::M-1] == K).any(axis=0).all()

It is faster still (for the array size given) to just compute a==K for the whole array:

k = a == K
k[::N-1, :].any(axis=1).all() and k[:, ::M-1].any(axis=0).all()

Timings are: 8.7us for the original in the question, 6.2us for the first way and 4.8us for the second, for M, N, K = 31, 31, 5.

April 3, 2026 Score: 1 Rep: 11 Quality: Low Completeness: 60%

I am aware that this wasn't really the question, but it bugs me a bit.

Checking all items in the bordering vectors for the purpose of knowing if any match, is overkill. If we only want to know whether there is one match, we can stop checking the vector as soon as we found one. To do this, we can implement a small function.

def containsk(arr, K):
    for a in arr:
        if a == K:
            return True
    return False

This won't have any noticeable difference in runtime for the given example, but if we enlarge the dataset a notable difference quickly emerges.

If we arbitrarily change the value of N to 29999 it takes:

np.any(a[0, :]==K) & np.any(a[-1, :]==K) & np.any(a[:, 0]==K) & np.any(a[:, -1]==K)

1.2 seconds to complete, while

containsk(a[0, :],K) & containsk(a[-1, :],K) & containsk(a[:, 0], K) & contains_k(a[:, -1],K)

needs only 0.1 second to complete.