# How to delete rows from a matrix that equals entries from another matrix?

**URL:** <https://discuss.python.org/t/how-to-delete-rows-from-a-matrix-that-equals-entries-from-another-matrix/15823>\
**Category:** Python Help\
**Created:** [May 18, 2022, 4:38am UTC](https://discuss.python.org/t/how-to-delete-rows-from-a-matrix-that-equals-entries-from-another-matrix/15823 "2022-05-18T04:38:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosephK](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@JosephK](https://discuss.python.org/u/JosephK)\
**Post date:** [May 18, 2022, 4:38am UTC](https://discuss.python.org/t/how-to-delete-rows-from-a-matrix-that-equals-entries-from-another-matrix/15823/1 "2022-05-18T04:38:19Z")

</div>

> I have two matrices. I want to delete rows from Matrix 1 that equals Matrix 2.
> 
> ```
> Matrix 1:
> [[2.00000000e+00 0.00000000e+00]
> [1.84775907e+00 7.65366865e-01]
> [1.41421356e+00 1.41421356e+00]
> [7.65366865e-01 1.84775907e+00]
> [1.22464680e-16 2.00000000e+00]
> [-7.65366865e-01 1.84775907e+00]
> [-1.41421356e+00 1.41421356e+00]
> [-1.84775907e+00 7.65366865e-01]
> [-2.00000000e+00 2.44929360e-16]
> [-1.84775907e+00 -7.65366865e-01]
> [-1.41421356e+00 -1.41421356e+00]
> [-7.65366865e-01 -1.84775907e+00]
> [-3.67394040e-16 -2.00000000e+00]
> [7.65366865e-01 -1.84775907e+00]
> [1.41421356e+00 -1.41421356e+00]
> [1.84775907e+00 -7.65366865e-01]
> [7.07106781e-01 7.07106781e-01]
> [1.00000000e+00 0.00000000e+00]
> [7.07106781e-01 -7.07106781e-01]
> [-1.83697020e-16 -1.00000000e+00]
> [-7.07106781e-01 -7.07106781e-01]
> [-1.00000000e+00 1.22464680e-16]
> [-7.07106781e-01 7.07106781e-01]
> [6.12323400e-17 1.00000000e+00]
> [1.28779197e+00 -5.33420900e-01]
> [2.98368551e-01 -1.50000000e+00]
> [-2.98368551e-01 -1.50000000e+00]
> [-2.98368551e-01 1.50000000e+00]
> [-8.49681746e-01 1.27163860e+00]
> [-1.28779197e+00 5.33420900e-01]
> [-1.27163860e+00 -8.49681746e-01]
> [-1.50000000e+00 -2.98368551e-01]
> [5.33420900e-01 1.28779197e+00]
> [1.28779197e+00 5.33420900e-01]
> [-8.30681297e-01 -1.27260907e+00]
> [8.30681297e-01 -1.27260907e+00]
> [1.50000000e+00 7.45847266e-02]
> [1.00792081e+00 1.11339954e+00]
> [-3.53553391e-01 8.53553391e-01]
> [-8.53553391e-01 -3.53553391e-01]
> [1.20712467e+00 -9.94468287e-01]
> [1.92387953e+00 -3.82683432e-01]
> [-1.63098631e+00 1.08979021e+00]
> [1.63098631e+00 1.08979021e+00]
> [1.61133220e-01 1.56482204e+00]
> [-1.56482204e+00 1.61133220e-01]
> [3.53553391e-01 -8.53553391e-01]
> [-3.53553391e-01 -8.53553391e-01]
> [-1.08471235e+00 9.12010725e-01]] 49
> 
> ```
> 
> Matrix 2:
> 
> ```
> [[2.00000000e+00 0.00000000e+00]
> [1.84775907e+00 7.65366865e-01]
> [1.92387953e+00 -3.82683432e-01]
> [1.63098631e+00 1.08979021e+00]
> [1.41421356e+00 1.41421356e+00]
> [7.65366865e-01 1.84775907e+00]
> [1.22464680e-16 2.00000000e+00]
> [-7.65366865e-01 1.84775907e+00]
> [-1.41421356e+00 1.41421356e+00]
> [-1.63098631e+00 1.08979021e+00]
> [-1.84775907e+00 7.65366865e-01]
> [-2.00000000e+00 2.44929360e-16]
> [-1.84775907e+00 -7.65366865e-01]
> [-1.41421356e+00 -1.41421356e+00]
> [-7.65366865e-01 -1.84775907e+00]
> [-3.67394040e-16 -2.00000000e+00]
> [7.65366865e-01 -1.84775907e+00]
> [1.41421356e+00 -1.41421356e+00]
> [1.84775907e+00 -7.65366865e-01]
> [7.07106781e-01 7.07106781e-01]
> [1.00000000e+00 0.00000000e+00]
> [6.12323400e-17 1.00000000e+00]
> [7.07106781e-01 -7.07106781e-01]
> [3.53553391e-01 -8.53553391e-01]
> [-1.83697020e-16 -1.00000000e+00]
> [-3.53553391e-01 -8.53553391e-01]
> [-7.07106781e-01 -7.07106781e-01]
> [-8.53553391e-01 -3.53553391e-01]
> [-1.00000000e+00 1.22464680e-16]
> [-7.07106781e-01 7.07106781e-01]
> [-3.53553391e-01 8.53553391e-01]] 31
> 
> ```
> 
> I tried different numpy functions but nothing works. For example,  
> numpy.delete or numpy.pop don’t work. I want to delete from Matrix 1  
> the elements that equal entries from Matrix 2. I am not sure how to do  
> this.

---

<div class="post-metadata">

**Author:** ![lvc](https://avatars.discourse-cdn.com/v4/letter/l/8edcca/32.png) [@lvc](https://discuss.python.org/u/lvc)\
**Post date:** [May 18, 2022, 5:57am UTC](https://discuss.python.org/t/how-to-delete-rows-from-a-matrix-that-equals-entries-from-another-matrix/15823/2 "2022-05-18T05:57:22Z")

</div>

Assuming your matrices are numpy arrays, you’ll notice that the result of `matrix1 == matrix2` is another array of element-wise results. You can call `.all(1)` on that to turn it into rowwise results, which means the rows of matrix1 that are equal rowwise to matrix2 are `matrix1[(matrix1 == matrix2).all(1)]`.  
This is rows you want to lose, which means the rows you want to _keep_ must be:

```auto
updated_matrix1 = matrix1[~(matrix1 == matrix2).all(1)]

```

---

<div class="post-metadata">

**Author:** ![JosephK](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@JosephK](https://discuss.python.org/u/JosephK)\
**Post date:** [May 18, 2022, 7:14am UTC](https://discuss.python.org/t/how-to-delete-rows-from-a-matrix-that-equals-entries-from-another-matrix/15823/3 "2022-05-18T07:14:01Z")

</div>

Hi @lvc ,

Thanks for the reply and the suggestion. That’s a smart code. I came up with another solution which is not that elegant. Here is what I came up with:

```auto
indices = []
indexlist = np.array(indices)

for i in range(0,len(matrix1[0])):
  for j in range(0,len(matrix2[0])):

    if matrix1()[i][0]==matrix2()[j][0] and matrix1()[i][1]==matrix2()[j][1]:
      
      indexlist = np.append(indexlist,i)

matrix3=np.delete(matrix1, [indexlist[:]], 0)

```

But your solution is much more elegant.
