bitwise or'ing rows

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

bitwise or'ing rows

Mads Ipsen
Hi

If I have an n x m array of bools, is there a handy way for me to
perform a 'bitwise_and' or 'bitwise_or' along an axis, for example all
the rows or all the columns? For example

a =
[[1,0,0,0],
  [0,0,1,0],
  [0,0,0,0]] (0 and 1 meaning True and False)

a.bitwise_or(axis=0)

giving

[1,0,1,0]

Best regards,

Mads




--
+---------------------------------------------------------------------+
| Mads Ipsen                                                          |
+----------------------------------+----------------------------------+
| Overgaden Oven Vandet 106, 4.tv  | phone:              +45-29716388 |
| DK-1415 København K              | email:      [hidden email] |
| Denmark                          | map  : https://goo.gl/maps/oQ6y6 |
+----------------------------------+----------------------------------+
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: bitwise or'ing rows

Jaime Fernández del Río
On Tue, Apr 4, 2017 at 9:14 AM, Mads Ipsen <[hidden email]> wrote:
Hi

If I have an n x m array of bools, is there a handy way for me to perform a 'bitwise_and' or 'bitwise_or' along an axis, for example all the rows or all the columns? For example

a =
[[1,0,0,0],
 [0,0,1,0],
 [0,0,0,0]] (0 and 1 meaning True and False)

a.bitwise_or(axis=0)

giving

[1,0,1,0]

I think what you want is equivalent to np.all(a, axis=0) for bitwise_and and np.any(a, axis=0) for bitwise_or.

You can also use the more verbose np.bitwise_and.reduce(a, axis=0) and np.bitwise_or.reduce(a, axis=0).

Jaime
 

Best regards,

Mads




--
+---------------------------------------------------------------------+
| Mads Ipsen                                                          |
+----------------------------------+----------------------------------+
| Overgaden Oven Vandet 106, 4.tv  | phone:              <a href="tel:%2B45-29716388" value="+4529716388" target="_blank">+45-29716388 |
| DK-1415 København K              | email:      [hidden email] |
| Denmark                          | map  : https://goo.gl/maps/oQ6y6 |
+----------------------------------+----------------------------------+
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion



--
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: bitwise or'ing rows

Mads Ipsen
Thanks!

On 04/04/2017 09:49 AM, Jaime Fernández del Río wrote:

> On Tue, Apr 4, 2017 at 9:14 AM, Mads Ipsen <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi
>
>     If I have an n x m array of bools, is there a handy way for me to
>     perform a 'bitwise_and' or 'bitwise_or' along an axis, for example
>     all the rows or all the columns? For example
>
>     a =
>     [[1,0,0,0],
>      [0,0,1,0],
>      [0,0,0,0]] (0 and 1 meaning True and False)
>
>     a.bitwise_or(axis=0)
>
>     giving
>
>     [1,0,1,0]
>
>
> I think what you want is equivalent to np.all(a, axis=0) for bitwise_and
> and np.any(a, axis=0) for bitwise_or.
>
> You can also use the more verbose np.bitwise_and.reduce(a, axis=0) and
> np.bitwise_or.reduce(a, axis=0).
>
> Jaime
>
>
>
>     Best regards,
>
>     Mads
>
>
>
>
>     --
>     +---------------------------------------------------------------------+
>     | Mads Ipsen                                                          |
>     +----------------------------------+----------------------------------+
>     | Overgaden Oven Vandet 106, 4.tv <http://4.tv>  | phone:
>       +45-29716388 <tel:%2B45-29716388> |
>     | DK-1415 København K              | email:
>     [hidden email] <mailto:[hidden email]> |
>     | Denmark                          | map  : https://goo.gl/maps/oQ6y6 |
>     +----------------------------------+----------------------------------+
>     _______________________________________________
>     NumPy-Discussion mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://mail.python.org/mailman/listinfo/numpy-discussion
>     <https://mail.python.org/mailman/listinfo/numpy-discussion>
>
>
>
>
> --
> (\__/)
> ( O.o)
> ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus
> planes de dominación mundial.
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion
>

--
+---------------------------------------------------------------------+
| Mads Ipsen                                                          |
+----------------------------------+----------------------------------+
| Overgaden Oven Vandet 106, 4.tv  | phone:              +45-29716388 |
| DK-1415 København K              | email:      [hidden email] |
| Denmark                          | map  : https://goo.gl/maps/oQ6y6 |
+----------------------------------+----------------------------------+
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion