# How to map two matrix by each element and perform simple mathematical operation by each row?

I have two matrix A and B of equal dimensions see below. In A matrix I have the variables a,b,c,d which have direct correspondence with matrix B element by each row. In other words, for first row {a, b, c, d} we have {2, 9, 6, 7}, further for each element in both row a=2, b=9, c=6 and d=7 similarly for other rows in both matrix.

A={{a, b, c, d}, {d, c, b, a}, {a, c, b, d}};
B={{2, 9, 6, 7}, {11, 3, 5, 12}, {12, 4, 1, 4}};

After mapping these two matrix, I want to perform simple mathematical operations (addition and subtraction). For example, for first row:

x1=a-d=2-7=-5
y1=b-a=9-2=7

similarly fir second row,

x2=a-d=12-11=1
y2=b-a=5-12=-7

I can map these two matrix by Map[A,B], but I don´t know how to map each element of both matrix. Is there a way we can map each element and then by using loop we evaluate a-d, b-a for each row?

• Look at what {{a, b, c, d}, {a, b, c, d}, {a, b, c, d}}.{{1, -1}, {0, 1}, {0, 0}, {-1, 0}} does, and see if you can adapt this to your problem. May 12, 2020 at 13:18
• Hi @J.M., in this way I couldn't reach to the solution, because In each row the order of variables (a,b,c,d) are changing. Further, list manipulation Function[A] /@ B gives wrong values. Actually, if we create somehow element by element correspondence then your solution might work.
– Aman
May 12, 2020 at 13:53

I didn't immediately give the full answer, in the hope someone would follow up on the hint in my comment. Anyway, the missing piece is to use Ordering[] to rearrange list B, like so:

MapThread[#1[[Ordering[#2]]] &, {B, A}].{{1, -1}, {0, 1}, {0, 0}, {-1, 0}}
{{-5, 7}, {1, -7}, {8, -11}}

A second method is to convert the data to an association, which can then be used for lookups:

{{-5, 7}, {1, -7}, {8, -11}}
• Hi J.M, Thank you very much for your help.
– Aman
May 16, 2020 at 0:31
• Hi J.M, I am trying to import all the variables in A by: A = Import[ "directory", "Table"], while I import these variable I couldn't apply your solution.
– Aman
May 16, 2020 at 1:43

MapThread[Block[{a, b, c, d}, # = #2; {a - d, b - a}] &, {A, B}]
(* {{-5, 7}, {1, -7}, {8, -11}} *)
• Thank you very much for your help.
– Aman
May 16, 2020 at 0:37

Here is a way using ReplaceAll (/.):

{a - d, b - a} /. MapThread[Rule, {A, B}, 2]

(* {{-5, 7}, {1, -7}, {8, -11}} *)
• Thank you very much. Its what I need.
– Aman
May 16, 2020 at 0:36

I use loop as a last resort in Mathematica and I believe there are other ways to solve this problem. However, I am using loop for this problem as it is quite intuitive. Please, let me know if you have trouble understanding the soln:

A = {{a, b, c, d}, {d, c, b, a}, {a, c, b, d}};
B = {{2, 9, 6, 7}, {11, 3, 5, 12}, {12, 4, 1, 4}};

NN = Length[A];
data = {};
For[i = 1, i <= NN,
a =.; b =.; c =.; d =.;
Evaluate[A[[i]]] = B[[i]];
values = AppendTo[data, {a - d, b - a}];
i++];
values
(*{{-5, 7}, {1, -7}, {8, -11}}*)
• Thank you @maeniss, I understand your loop.
– Aman
May 16, 2020 at 0:29