Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab isempty() function in numpy?

Tags:

python

numpy

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)?

like image 320
gustavgans Avatar asked Nov 28 '14 08:11

gustavgans


2 Answers

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
like image 198
tttthomasssss Avatar answered Oct 21 '22 07:10

tttthomasssss


you can compare your array x, with 0 and see if all values are False

np.all(x==0)

like image 6
bakkal Avatar answered Oct 21 '22 06:10

bakkal