TransWikia.com

Comparing and Modifying two Matrices (list of list)

Mathematica Asked on October 22, 2021

I need to compare two matrices with the same number of columns but a different number of rows (please see an example of Matrices A and B). I would like to know how I can compare the second column of the first matrix with the second column of the second matrix and if there were any equal value substitute the associated array in the first column of the first matrix with the associated array in the first column of the second matrix and restore the first Matrix. For example

A = {{1, 2, 3, 5}, {4, 5, 6, 8}, {7, 8, 9, 3}, {3, 56, 8, 2}, {4, 5, 6,
    8}}
B = {{6, 7, 9, 1}, {2, 5, 0, 8}, {1, 2, 3, 7}, {34, 56, 78, 56}}

Considering that A[[2,2]]=B[[2,2]]=5 and A[[4,2]]=B[[4,2]]=56, after applying the above condition, the Matrix A should change to:

ANew= {{1, 2, 3, 5}, {2, 5, 6, 8}, {7, 8, 9, 3}, {34, 56, 8, 2}, {4, 5, 6,
    8}}

General case:

What if the same elements were located in different rows (for example):

A = {{1, 2, 3, 5}, {4, 5, 6, 8}, {7, 8, 9, 3}, {3, 56, 8, 2}, {4, 5, 
    6, 8}};

B = {{6, 7, 9, 1}, {2, 5, 0, 8}, {1, 56, 3, 7}, {34, 42, 78, 56}};

In this case, considering that A[[2,2]]=B[[2,2]]=5 and A[[4,2]]=B[[3,2]]=56, after applying the above condition, the Matrix A should change to:

ANew= {{1, 2, 3, 5}, {2, 5, 6, 8}, {7, 8, 9, 3}, {1, 56, 8, 2}, {4, 5, 6,
    8}}

One Answer

This script can be applied when the same elements are in the same row.

A = {{1, 2, 3, 5}, {4, 5, 6, 8}, {7, 8, 9, 3}, {3, 56, 8, 2}, {4, 5, 6, 8}};
B = {{6, 7, 9, 1}, {2, 5, 0, 8}, {1, 2, 3, 7}, {34, 56, 78, 56}};

l = Length /@ {A, B} // Min
(* 4 *)
truefalse = MapThread[Equal, {A[[;; l, 2]], B[[;; l, 2]]}]
(* {False, True, False, True} *)
index = Pick[Range[l], truefalse]
(* {2, 4} *)
Anew = A;
Anew[[index, 1]] = Pick[B[[;; l, 1]], truefalse]; Anew

Answered by xzczd on October 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP