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,
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
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.
My code examples are always for Python >=3.6.0
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 key lookup in dictionary GaryNR 1 311 Jul-16-2020, 06:55 PM
Last Post: Gribouillis
  Partial using Tkinter function chesschaser 10 824 Jul-03-2020, 03:57 PM
Last Post: chesschaser
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 747 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Partial Word Search Kristenl2784 2 332 Jun-29-2020, 08:26 PM
Last Post: Kristenl2784
  Application of dilute matrices chris_drak 0 278 Mar-29-2020, 03:04 PM
Last Post: chris_drak
  Get partial string from os.system() zinho 1 390 Nov-06-2019, 11:20 AM
Last Post: zinho
  functool - partial errors SheeppOSU 1 581 Apr-27-2019, 02:12 AM
Last Post: rxndy
  Partial convertion string to int in lists satellite89 6 836 Apr-22-2019, 08:50 PM
Last Post: Yoriz
  Flipping matrices problem Qmohankumar0017 10 1,237 Feb-20-2019, 06:47 PM
Last Post: buran
  matrices math problem lokoprof 1 855 Aug-27-2018, 07:48 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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