[Numpy-discussion] logic problem
Warren Weckesser
warren.weckesser at enthought.com
Mon Jan 18 14:39:30 EST 2010
josef.pktd at gmail.com wrote:
> On Mon, Jan 18, 2010 at 2:18 PM, Warren Weckesser
> <warren.weckesser at enthought.com> wrote:
>
>> Ernest Adrogué wrote:
>>
>>> Hi,
>>>
>>> This is hard to explain. In this code:
>>>
>>> reduce(np.logical_or, [m1 & m2, m1 & m3, m2 & m3])
>>>
>>> where m1, m2 and m3 are boolean arrays, I'm trying to figure
>>> out an expression that works with an arbitrary number of
>>> arrays, not just 3. Any idea??
>>>
>>>
>>>
>> If I understand the problem correctly, you want the result to be True
>> whenever any pair of the corresponding elements of the arrays are True.
>>
>> This could work:
>>
>> reduce(np.add, [m.astype(int) for m in mlist]) > 1
>>
>> where mlist is a list of the boolean array (e.g. mlist = [m1, m2, m3] in
>> your example).
>>
>
> much nicer than what I came up with. Does iterator instead of
> intermediate list work (same for my list comprehension)?
>
> reduce(np.add, (m.astype(int) for m in mlist)) > 1
>
>
Yes, that works and is preferable, especially if the arrays are large or
the list is long.
Warren
> Josef
>
>
>
More information about the NumPy-Discussion
mailing list