Python Forum
Partial "visual" Matching of matrices
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Partial "visual" Matching of matrices
#14
from itertools import chain


# converting your input data to a valid Python object:
A = """
1 0 1 1 1 0 0 1
1 0 0 1 1 0 0 1
1 0 1 0 0 0 0 0
1 0 0 0 1 0 0 1
""".strip()

B = """
0 1 0 0 1 0 0 1
1 1 0 0 0 0 0 1
1 1 1 0 0 0 0 1
1 0 1 0 1 0 0 1
""".strip()

C = """
1 0 1 0 1 0 0 1
1 0 1 1 1 0 0 1
1 0 1 0 0 0 0 1
1 1 0 0 1 0 0 1
""".strip()


# nested list comprehension to create the matrix
a = [[int(d) for d in a.split()] for a in A.splitlines()]
b = [[int(d) for d in b.split()] for b in B.splitlines()]
c = [[int(d) for d in c.split()] for c in C.splitlines()]


# here are the functions

def equality1(matrix1, matrix2):
    result = 0
    for row1, row2 in zip(matrix1, matrix2):
        for col1, col2 in zip(row1, row2):
            if col1 == col2:
                result += 1
    return result


def equality2(matrix1, matrix2):
    result = 0
    for m1, m2 in zip(chain.from_iterable(matrix1), chain.from_iterable(matrix2)):
        if m1 == m2:
            result += 1
    return result
You don't have to understand the upper part with the nested list comprehensions.
The function equality1 and equality2 should be descriptive enough, to understand it.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Partial "visual" Matching of matrices - by DeaD_EyE - Nov-02-2019, 09:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  partial functions before knowing the values mikisDeWitte 4 656 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Move Files based on partial Match mohamedsalih12 2 860 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Partial KEY search in dict klatlap 6 1,321 Mar-28-2023, 07:24 AM
Last Post: buran
  remove partial duplicates from csv ledgreve 0 820 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Webhook, post_data, GPIO partial changes DigitalID 2 1,017 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  4D matrices ali1almakhmari 0 770 Jun-13-2022, 09:21 AM
Last Post: ali1almakhmari
  Optimal way to search partial correspondence in a large dict genny92c 0 1,018 Apr-22-2022, 10:20 AM
Last Post: genny92c
  Unable to use Pauli Matrices in QNET Package Rupayan 2 1,932 Sep-25-2021, 06:02 AM
Last Post: Rupayan
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,400 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Partial key lookup in dictionary GaryNR 1 3,484 Jul-16-2020, 06:55 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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