Python Forum
Check if two matrix are equal and of not add the matrix to the list
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if two matrix are equal and of not add the matrix to the list
#1
I have a list which contains may matrices. I called this list as su4_np and it contains more than 500 matrices
When I want to add a matrix to the list, I want to check if there is any matrix in the list which is identical to the matrix that I want to add. If there is no matrix identical to the matrix which I want to add, then add the matrix to the list:

I simply wrote the following scripts:

deneme = np.array([[-0.30902099609375 - 0.5j, 0, -0.8089599609375j, 0],
       [0, -0.30902099609375 - 0.5j, 0, -0.8089599609375j],
       [-0.8089599609375j, 0, -0.30902099609375 + 0.5j, 0],
       [0, -0.8089599609375j, 0, -0.30902099609375 + 0.5j]],dtype=object)

if any(np.array_equal(deneme, in_su4) for in_su4 in su4_np):
    print(mat)
    print(deneme)  
where su4_np is my list. However, the scripts prints the two matrix as if they are equal. But they are not equal:

The output:

[[-0.500000000000000 0 -0.30902099609375 - 0.8089599609375*I 0]
[0 -0.500000000000000 0 -0.30902099609375 - 0.8089599609375*I]
[0.30902099609375 - 0.8089599609375*I 0 -0.500000000000000 0]
[0 0.30902099609375 - 0.8089599609375*I 0 -0.500000000000000]]


[[(-0.30902099609375-0.5j) 0 (-0-0.8089599609375j) 0]
[0 (-0.30902099609375-0.5j) 0 (-0-0.8089599609375j)]
[(-0-0.8089599609375j) 0 (-0.30902099609375+0.5j) 0]
[0 (-0-0.8089599609375j) 0 (-0.30902099609375+0.5j)]]

The matrices are not equal!! How to solve this issue?
Thanks in advance
Reply
#2
When you find a match you don't print the matching matrix, you print mat. Try this:
for in_su4 in su4_np:
    if np.array_equal(deneme, in_su4):
        print(in_su4)
        print(deneme)
        break
else:
    su4_np.append(deneme)
Reply
#3
for in_su4 in su4_np:
    if np.array_equal(deneme, in_su4):
        print(in_su4)
        print(deneme)
        break
    else:
        su4_np.append(deneme)
Reply
#4
This does not work.
(Jul-10-2023, 01:46 AM)ICanIBB Wrote:
for in_su4 in su4_np:
    if np.array_equal(deneme, in_su4):
        print(in_su4)
        print(deneme)
        break
    else:
        su4_np.append(deneme)
The code appends deneme to su4_np each in_su4 does not match deneme. This likely means deneme will be appended multiple times. You can see that here:
x = [1, 5, 9]
y = 9

for value in x:
    if value != y:
        x.append(y)

print(x)
Output:
[1, 5, 9, 9, 9]
Even though 9 is in x, 9 != 1 and 9 != 5. Each time y is not equal to a value in x, y is appended to x.

Back to the original question, deneme should only be appended to su4_np if deneme was checked against every matrix in su4_np, and not of the matrices matched deneme.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with scrolling text on RGB Matrix Foutsy 3 304 Apr-09-2024, 09:00 PM
Last Post: deanhystad
  Help with to check an Input list data with a data read from an external source sacharyya 3 417 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 719 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  Help with Python Script to generate SVG Dot Matrix Pattern for LED Light Guide iamrickm 2 772 Aug-25-2023, 06:07 PM
Last Post: iamrickm
  Print confusion matrix MrSonoa 1 922 Apr-20-2023, 04:17 PM
Last Post: farshid
  Can't modify a simple matrix Lewkiy 3 919 Feb-05-2023, 02:59 PM
Last Post: Lewkiy
  matrix multiplication term by term karolina 1 833 Dec-10-2022, 10:11 AM
Last Post: Gribouillis
  Regarding how to randomizing list with having equal probability radraw 14 2,208 Nov-06-2022, 11:09 PM
Last Post: Pedroski55
  detect equal sequences in list flash77 17 2,849 Oct-28-2022, 06:38 AM
Last Post: flash77
  is there equal syntax to "dir /s /b" kucingkembar 2 1,002 Aug-16-2022, 08:26 AM
Last Post: kucingkembar

Forum Jump:

User Panel Messages

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