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


Messages In This Thread
RE: Partial "visual" Matching of matrices - by newbieAuggie2019 - Nov-01-2019, 11:51 PM

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