![](https://secure.gravatar.com/avatar/1ff3f37e77320e9b57d7f1b611953acf.jpg?s=120&d=mm&r=g)
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?? Bye.
![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
2010/1/18 Ernest Adrogué <eadrogue@gmx.net>:
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??
What's the shape of mi (dimension)? fixed or arbitrary number of dimension? a loop is the most memory efficient array broadcasting builds large arrays (and maybe has redundant calculations), but might be a one-liner or something like list comprehension m = [m1, m2, ... mn] reduce(np.logical_or, [mi & mj for (i, mi) in enumerate(m) for (j, mj) in enumerate(m) if i<j ])
m = [np.arange(10)<5, np.arange(10)>3, np.arange(10)>8] m [array([ True, True, True, True, True, False, False, False, False, False], dtype=bool), array([False, False, False, False, True, True, True, True, True, True], dtype=bool), array([False, False, False, False, False, False, False, False, False, True], dtype=bool)]
reduce(np.logical_or, [mi & mj for (i, mi) in enumerate(m) for (j, mj) in enumerate(m) if i<j ]) array([False, False, False, False, True, False, False, False, False, True], dtype=bool)
Josef
Bye. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/1ff3f37e77320e9b57d7f1b611953acf.jpg?s=120&d=mm&r=g)
18/01/10 @ 14:17 (-0500), thus spake josef.pktd@gmail.com:
2010/1/18 Ernest Adrogué <eadrogue@gmx.net>:
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??
What's the shape of mi (dimension)? fixed or arbitrary number of dimension? a loop is the most memory efficient
I forgot to mention, mi are 1-dimensional, all the same length of course.
array broadcasting builds large arrays (and maybe has redundant calculations), but might be a one-liner
or something like list comprehension
m = [m1, m2, ... mn] reduce(np.logical_or, [mi & mj for (i, mi) in enumerate(m) for (j, mj) in enumerate(m) if i<j ])
Thanks! I was thinking of a list comprehensioni but it didn't occur to me how to avoid redundant combinations.
m = [np.arange(10)<5, np.arange(10)>3, np.arange(10)>8] m [array([ True, True, True, True, True, False, False, False, False, False], dtype=bool), array([False, False, False, False, True, True, True, True, True, True], dtype=bool), array([False, False, False, False, False, False, False, False, False, True], dtype=bool)]
reduce(np.logical_or, [mi & mj for (i, mi) in enumerate(m) for (j, mj) in enumerate(m) if i<j ]) array([False, False, False, False, True, False, False, False, False, True], dtype=bool)
Josef
Bye. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/b0f62d137f9ea1d0b6cc4e7e6f61b119.jpg?s=120&d=mm&r=g)
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). Warren
Bye. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
On Mon, Jan 18, 2010 at 2:18 PM, Warren Weckesser <warren.weckesser@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 Josef
Warren
Bye. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/b0f62d137f9ea1d0b6cc4e7e6f61b119.jpg?s=120&d=mm&r=g)
josef.pktd@gmail.com wrote:
On Mon, Jan 18, 2010 at 2:18 PM, Warren Weckesser <warren.weckesser@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
![](https://secure.gravatar.com/avatar/1ff3f37e77320e9b57d7f1b611953acf.jpg?s=120&d=mm&r=g)
18/01/10 @ 13:18 (-0600), thus spake Warren Weckesser:
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.
Exactly.
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).
Very clever. Thanks a lot!
Warren
Bye. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Ernest Adrogué
-
josef.pktd@gmail.com
-
Warren Weckesser