# Implementing element-wise logical and tensor operation

**URL:** <https://discuss.pytorch.org/t/implementing-element-wise-logical-and-tensor-operation/33596>\
**Category:** Uncategorized\
**Created:** [January 2, 2019, 1:19pm UTC](https://discuss.pytorch.org/t/implementing-element-wise-logical-and-tensor-operation/33596 "2019-01-02T13:19:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jici\_xing](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/5fc32e/32.png) [@jici\_xing](https://discuss.pytorch.org/u/jici_xing)\
**Post date:** [January 2, 2019, 1:19pm UTC](https://discuss.pytorch.org/t/implementing-element-wise-logical-and-tensor-operation/33596/1 "2019-01-02T13:19:05Z")

</div>

Hi ,  
I have 2 matrix that have same shape.I want to do element-wise logical\_and operation and return a matrix that match the original shape. How to do this?  
Thank you for your time and consideration!

---

<div class="post-metadata">

**Author:** ![yunusemre](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yunusemre/32/37214_2.png) [@yunusemre](https://discuss.pytorch.org/u/yunusemre)\
**Post date:** [January 2, 2019, 5:40pm UTC](https://discuss.pytorch.org/t/implementing-element-wise-logical-and-tensor-operation/33596/2 "2019-01-02T17:40:57Z")

</div>

> [@How to do elementwise multiplication of two vectors?](https://discuss.pytorch.org/t/how-to-do-elementwise-multiplication-of-two-vectors/13182):
>
> I have two vectors each of length n, I want element wise multiplication of two vectors. result will be a vector of length n.

Just use

> `a * b` or `torch.mul(a, b)`

---

<div class="post-metadata">

**Author:** ![zhl515](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhl515/32/27796_2.png) [@zhl515](https://discuss.pytorch.org/u/zhl515)\
**Post date:** [January 3, 2019, 6:12am UTC](https://discuss.pytorch.org/t/implementing-element-wise-logical-and-tensor-operation/33596/3 "2019-01-03T06:12:40Z")

</div>

Torch.dtype is the data type of torch.tensor, the torch.dtype doesn’t have bool type, similar to bool type is torch.uint8, your can see torch.dtype [here](https://pytorch.org/docs/stable/tensor_attributes.html). So if do logical\_and operation on two tensor, you should expect to get 0/1s numerical values not True/False bool values. And tensor with uint8 type have logical\_and operation &, you need change the tensor data type to unint8 first.

```auto
a = torch.tensor([[0, 1], [0.0, 2.3]])
b = torch.tensor([[1, 1], [0.0, 0.0]])
c = torch.zeros_like(a) # all zero with same shape of a
result = (~torch.eq(a, c)) & (~torch.eq(b, c))
'''
result is a 
 tensor([[0, 1],
        [0, 0]], dtype=torch.uint8)
'''
result.numpy().astype('bool') # change to bool ndarray in numpy

```

---

<div class="post-metadata">

**Author:** ![drevicko](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/drevicko/32/14854_2.png) [@drevicko](https://discuss.pytorch.org/u/drevicko)\
**Post date:** [January 24, 2020, 4:26am UTC](https://discuss.pytorch.org/t/implementing-element-wise-logical-and-tensor-operation/33596/4 "2020-01-24T04:26:13Z")

</div>

@zhl515 since pytorch 1.2, there is a dtype torch.bool and associated BoolTensor. The bitwise operators aka numpy (and &, or |, xor ^ and not ~) all work on these as logical operations.
