[Numpy-discussion] logic problem

Ernest Adrogué eadrogue@gmx....
Mon Jan 18 13:48:13 CST 2010


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


More information about the NumPy-Discussion mailing list