Python Forum

Full Version: Partial "visual" Matching of matrices
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,
I have this issue and i am a bit short on ideas on how to resolve it
Suppose you have 3 matrices :
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
At first forget everything about algebra ... i am not after getting any value/solution/simplification what so ever...the deal here is to find the best "visual" match...in this case Matrix A is more matching to Matrix C in comparison to B.
The important thing that no change in order/values should take place...the matrices will remain unaffected no matter what but i need to find that Matrix A differs only to 4 elements compared to C and compared to B differs in 10 elements so the best match is C
P.S. sorry for the bad format of the matrices... i tried to find an appropriate bbcode to format them but no luck
What exactly is the question?
How to find the match

Well i think i might have an idea...
I will subtract the matrices and count the values different to zero.... hmmm...maybe i will try that....
The obvious idea is to count the entries that differ, but an efficient implementation may depend crucially on the data structure that you have in memory. How do the matrices appear in your program?
Exactly as you see them..
masteripper Wrote:Exactly as you see them..
This is not possible because it is not python code. Post the code and what you've tried to solve the problem.
The answer is C.
C has more equal elements with A as B with A.

I solved it with a nested loop, but you can also serialize the matrix with itertools.chain.from_iterable().
You need zip to get from matrix1 and matrix2 for each iteration two elements.
(Nov-01-2019, 12:54 PM)DeaD_EyE Wrote: [ -> ]The answer is C.
C has more equal elements with A as B with A.

I solved it with a nested loop, but you can also serialize the matrix with itertools.chain.from_iterable().
You need zip to get from matrix1 and matrix2 for each iteration two elements.

Thanks man..can you give me some more code

(Nov-01-2019, 12:16 PM)Gribouillis Wrote: [ -> ]
masteripper Wrote:Exactly as you see them..
This is not possible because it is not python code. Post the code and what you've tried to solve the problem.

If i had the concept i could have code it....this is not issue in most cases...but the thing was to grasp the concept...the subtraction is one way... if something better comes it will be great.... (i really didn't thought about subtraction until i posted the thread and i was looking...for start the data are inside pandas dataframes so i have found out that i can subtract them )
(Nov-01-2019, 10:22 AM)masteripper Wrote: [ -> ]Suppose you have 3 matrices :
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

[ ... ] in this case Matrix A is more matching to Matrix C in comparison to B.

Hi!

My approach would be comparing matrix A and matrix B, element by element, and keep the number of elements in common in a variable, named, let's say commonElementsAB.

Then, I would do the same, comparing matrix A and matrix C, element by element, and keep the number of elements in common in a variable, named, let's say commonElementsAC.

After that, I would compare the value of commonElementsAB with the value of commonElementsAC. If the value of commonElementsAB is greater than the value of commonElementsAC, that means that Matrix A is more matching to Matrix B than to Matrix C. If the value of commonElementsAC is greater than the value of commonElementsAB, that means that Matrix A is more matching to Matrix C than to Matrix B.

All the best,
(Nov-01-2019, 08:33 PM)newbieAuggie2019 Wrote: [ -> ]
(Nov-01-2019, 10:22 AM)masteripper Wrote: [ -> ]Suppose you have 3 matrices :
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

[ ... ] in this case Matrix A is more matching to Matrix C in comparison to B.

Hi!

My approach would be comparing matrix A and matrix B, element by element, and keep the number of elements in common in a variable, named, let's say commonElementsAB.

Then, I would do the same, comparing matrix A and matrix C, element by element, and keep the number of elements in common in a variable, named, let's say commonElementsAC.

After that, I would compare the value of commonElementsAB with the value of commonElementsAC. If the value of commonElementsAB is greater than the value of commonElementsAC, that means that Matrix A is more matching to Matrix B than to Matrix C. If the value of commonElementsAC is greater than the value of commonElementsAB, that means that Matrix A is more matching to Matrix C than to Matrix B.

All the best,
I am thinking that this might be slower...but thanks for replying.
Pages: 1 2