Python Forum
Partial "visual" Matching of matrices
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Partial "visual" Matching of matrices
#11
(Nov-01-2019, 09:20 PM)masteripper Wrote: I am thinking that this might be slower...but thanks for replying.

Hi again!

My answer is probably quite verbose, and as I am a newbie, I'm sure that there must be better ways to do it than my code, but I made the program give a visual and self-explanatory output:
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]]

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]]

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]]

commonElementsAB = []
commonElementsAC = []

for i in range(4):
    for j in range(8):
        if (A[i][j] == B[i][j]) : 
            commonElementsAB.append(A[i][j]) 
        else : 
            pass

print("\nThis is matrix A:\n")
for elements in A:
    print(*elements)
print("\nThis is matrix B:\n")
for elements in B:
    print(*elements)
print(f"\nThe elements in common in the same \n\
position in matrices A and B are {len(commonElementsAB)}.\n\
These {len(commonElementsAB)} elements in common are: \n\
{commonElementsAB}.\n")

for i in range(4) : 
    for j in range(8) : 
        if (A[i][j] == B[i][j]) : 
            print(A[i][j], end = " ") 
        else : 
            print("*", end = " ")  
    print()

for x in range(4):
    for y in range(8):
        if (A[x][y] == C[x][y]) : 
            commonElementsAC.append(A[x][y]) 
        else : 
            pass

print("\nThis is matrix A:\n")
for elements in A:
    print(*elements)
print("\nThis is matrix C:\n")
for elements in C:
    print(*elements)
print(f"\nThe elements in common in the same \n\
position in matrices A and C are {len(commonElementsAC)}.\n\
These {len(commonElementsAC)} elements in common are: \n\
{commonElementsAC}.\n")

for i in range(4) : 
    for j in range(8) : 
        if (A[i][j] == C[i][j]) : 
            print(A[i][j], end = " ") 
        else : 
            print("*", end = " ")  
    print()

if len(commonElementsAB) == len(commonElementsAC):
    print(f"\nBoth matrices B and C have the same number of\n\
common elements with matrix A.")
elif len(commonElementsAB) > len(commonElementsAC):
    print(f"\nMatrices A and B have a bigger number of\n\
common elements in the same position than\n\
matrices A and C.")    
elif len(commonElementsAB) < len(commonElementsAC):
    print(f"\nMatrices A and C have a bigger number of\n\
common elements in the same position than\n\
matrices A and B.")
and the output is:
Output:
This is matrix 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 This is matrix 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 The elements in common in the same position in matrices A and B are 22. These 22 elements in common are: [1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1]. * * * * 1 0 0 1 1 * 0 * * 0 0 1 1 * 1 0 0 0 0 * 1 0 * 0 1 0 0 1 This is matrix 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 This is matrix 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 The elements in common in the same position in matrices A and C are 28. These 28 elements in common are: [1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1]. 1 0 1 * 1 0 0 1 1 0 * 1 1 0 0 1 1 0 1 0 0 0 0 * 1 * 0 0 1 0 0 1 Matrices A and C have a bigger number of common elements in the same position than matrices A and B. >>>
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#12
(Nov-01-2019, 11:51 PM)newbieAuggie2019 Wrote:
(Nov-01-2019, 09:20 PM)masteripper Wrote: I am thinking that this might be slower...but thanks for replying.

Hi again!

My answer is probably quite verbose, and as I am a newbie, I'm sure that there must be better ways to do it than my code, but I made the program give a visual and self-explanatory output:
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]]

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]]

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]]

commonElementsAB = []
commonElementsAC = []

for i in range(4):
    for j in range(8):
        if (A[i][j] == B[i][j]) : 
            commonElementsAB.append(A[i][j]) 
        else : 
            pass

print("\nThis is matrix A:\n")
for elements in A:
    print(*elements)
print("\nThis is matrix B:\n")
for elements in B:
    print(*elements)
print(f"\nThe elements in common in the same \n\
position in matrices A and B are {len(commonElementsAB)}.\n\
These {len(commonElementsAB)} elements in common are: \n\
{commonElementsAB}.\n")

for i in range(4) : 
    for j in range(8) : 
        if (A[i][j] == B[i][j]) : 
            print(A[i][j], end = " ") 
        else : 
            print("*", end = " ")  
    print()

for x in range(4):
    for y in range(8):
        if (A[x][y] == C[x][y]) : 
            commonElementsAC.append(A[x][y]) 
        else : 
            pass

print("\nThis is matrix A:\n")
for elements in A:
    print(*elements)
print("\nThis is matrix C:\n")
for elements in C:
    print(*elements)
print(f"\nThe elements in common in the same \n\
position in matrices A and C are {len(commonElementsAC)}.\n\
These {len(commonElementsAC)} elements in common are: \n\
{commonElementsAC}.\n")

for i in range(4) : 
    for j in range(8) : 
        if (A[i][j] == C[i][j]) : 
            print(A[i][j], end = " ") 
        else : 
            print("*", end = " ")  
    print()

if len(commonElementsAB) == len(commonElementsAC):
    print(f"\nBoth matrices B and C have the same number of\n\
common elements with matrix A.")
elif len(commonElementsAB) > len(commonElementsAC):
    print(f"\nMatrices A and B have a bigger number of\n\
common elements in the same position than\n\
matrices A and C.")    
elif len(commonElementsAB) < len(commonElementsAC):
    print(f"\nMatrices A and C have a bigger number of\n\
common elements in the same position than\n\
matrices A and B.")
and the output is:
Output:
This is matrix 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 This is matrix 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 The elements in common in the same position in matrices A and B are 22. These 22 elements in common are: [1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1]. * * * * 1 0 0 1 1 * 0 * * 0 0 1 1 * 1 0 0 0 0 * 1 0 * 0 1 0 0 1 This is matrix 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 This is matrix 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 The elements in common in the same position in matrices A and C are 28. These 28 elements in common are: [1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1]. 1 0 1 * 1 0 0 1 1 0 * 1 1 0 0 1 1 0 1 0 0 0 0 * 1 * 0 0 1 0 0 1 Matrices A and C have a bigger number of common elements in the same position than matrices A and B. >>>
All the best,

Thanks a lot man...
Reply
#13
(Nov-02-2019, 08:22 AM)masteripper Wrote: Thanks a lot man...

You're welcome! Wink
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#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
#15
(Nov-02-2019, 09:03 AM)DeaD_EyE Wrote:
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.

Thanks my friend
Reply
#16
Well thanks for the responses
With your help i have constructed the following (Pandas is great !!! )
import pandas as pd

def noOfDifferentElements(matrix1,matrix2):
    pinput1 = pd.DataFrame(matrix1)
    pinput2 = pd.DataFrame(matrix2)
    dfBool = (pinput1 != pinput2).stack()
    diff = pd.concat([pinput1.stack()[dfBool],pinput2.stack()[dfBool]],axis=1)
    print("No of Differece Matrix1 & Matrix2 : {difference}".format(difference=diff.size/2))

# 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()]


# Testing No of Differences between of the matrices
noOfDifferentElements(a,b)
noOfDifferentElements(b,c)
noOfDifferentElements(a,c)
and the winner is :
No of Differece Matrix1 & Matrix2 : 10.0
No of Differece Matrix1 & Matrix2 : 10.0
No of Differece Matrix1 & Matrix2 : 4.0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  partial functions before knowing the values mikisDeWitte 4 536 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Move Files based on partial Match mohamedsalih12 2 743 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Partial KEY search in dict klatlap 6 1,198 Mar-28-2023, 07:24 AM
Last Post: buran
  remove partial duplicates from csv ledgreve 0 745 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Webhook, post_data, GPIO partial changes DigitalID 2 953 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  4D matrices ali1almakhmari 0 734 Jun-13-2022, 09:21 AM
Last Post: ali1almakhmari
  Optimal way to search partial correspondence in a large dict genny92c 0 974 Apr-22-2022, 10:20 AM
Last Post: genny92c
  Unable to use Pauli Matrices in QNET Package Rupayan 2 1,877 Sep-25-2021, 06:02 AM
Last Post: Rupayan
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,337 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Partial key lookup in dictionary GaryNR 1 3,383 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