Python Forum
comparing each rows of two matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comparing each rows of two matrix
#11
Here is how you could print every pair of identical rows
from collections import defaultdict
import itertools as itt
import numpy as np
a=np.matrix([[1,2,3],[42,68,69],[1,2,3],[85,89,95]])
b=np.matrix([[42,68,69],[1,2,3],[85,89,95], [42,68,69]])
 
def group_lines(m):
    d = defaultdict(list)
    for i, row in enumerate(m.A):
        d[tuple(row)].append(i)
    return d

def identical_rows(ma, mb):
    da, db = (group_lines(m) for m in (ma, mb))
    for row, na in da.items():
        if row in db:
            yield from itt.product(na, db[row])

if __name__ == '__main__':
    for i, j in identical_rows(a, b):
        print(i, j, a.A[i])
The result is
Output:
3 2 [85 89 95] 1 0 [42 68 69] 1 3 [42 68 69] 0 1 [1 2 3] 2 1 [1 2 3]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020