[Numpy-discussion] filters for rows or columns

Giuseppe Aprea giuseppe.aprea@gmail....
Wed Aug 26 02:21:13 CDT 2009


On Wed, Aug 26, 2009 at 1:26 AM, Robert Kern<robert.kern@gmail.com> wrote:
> On Tue, Aug 25, 2009 at 16:18, Giuseppe Aprea<giuseppe.aprea@gmail.com> wrote:
>> Hi,  I would like to do something like this
>>
>> a=array([[1,2,3,4],[5,6,7,8],[4,5,6,0]])
>> idxList=[]
>> for i in range(0,a.shape[1]):
>>    if len(nonzero(a[:,i])[0])==1:   #want to extract column indices
>> of those columns which only have one non vanishing element
>>         idxList.append(i)
>>
>> I already used where on !D array but I don't know if there is some function or
>> some kind of syntax which allow you to evaluate a condition for each
>> column(row).
>
> column_mask = ((a != 0).sum(axis=1) == 1)
> idxArray = np.nonzero(column_mask)[0]  # if you must
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>  -- Umberto Eco
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

That's interesting. Thanks a lot! In my case that becames:

column_mask = ((a != 0).sum(axis=0) == 1)
idxArray = np.nonzero(column_mask)[0]

cheers

g


More information about the NumPy-Discussion mailing list