5
$\begingroup$

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?

Thanks in Advance

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ – J. M.'s torpor May 12 '20 at 13:18
  • $\begingroup$ 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. $\endgroup$ – Aman May 12 '20 at 13:53
5
$\begingroup$

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:

{#[a] - #[d], #[b] - #[a]} & /@ MapThread[AssociationThread, {A, B}]
   {{-5, 7}, {1, -7}, {8, -11}}
$\endgroup$
2
  • $\begingroup$ Hi J.M, Thank you very much for your help. $\endgroup$ – Aman May 16 '20 at 0:31
  • $\begingroup$ 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. $\endgroup$ – Aman May 16 '20 at 1:43
5
$\begingroup$

How about:

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

Here is a way using ReplaceAll (/.):

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

(* {{-5, 7}, {1, -7}, {8, -11}} *)
$\endgroup$
1
  • $\begingroup$ Thank you very much. Its what I need. $\endgroup$ – Aman May 16 '20 at 0:36
2
$\begingroup$

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}}*)
$\endgroup$
1
  • $\begingroup$ Thank you @maeniss, I understand your loop. $\endgroup$ – Aman May 16 '20 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.