I have this code in matlab:
switch 1
case isempty(A)
...
where A is a 2 dimension Array.
How can I check with numpy if a 2-dim Array is empty (only has 0 values)?
For checking if an array is empty (that is, it doesn't contain any elements), you can use A.size == 0
:
import numpy as np
In [2]: A = np.array([[1, 2], [3, 4]])
In [3]: A.size
Out[3]: 4
In [4]: B = np.array([[], []])
In [5]: B.size
Out[5]: 0
To check whether it only contains 0's you can check for np.count_nonzero(A)
:
In [13]: Y = np.array([[0, 0], [0, 0]])
In [14]: np.count_nonzero(Y)
Out[14]: 0
you can compare your array x
, with 0 and see if all values are False
np.all(x==0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With