3
$\begingroup$

i can substitute lists in list as follows ihave 3 lists

a = {1, 2, 3};
b = {2, 1, 3};
c = {1, 3, 2};

and i have a list w = {x + y + z, y, x + z};

i do it

w /. {x -> a, y -> b, z -> c}

the result is as follow

{{4, 6, 8}, {2, 1, 3}, {2, 5, 5}}

but i want it as follows

{x1+y1+z1,y2,x3+z3}={4, 1, 5}
$\endgroup$
3
  • $\begingroup$ What are a,b,c? $\endgroup$
    – lericr
    Sep 5, 2022 at 15:36
  • $\begingroup$ Oh, I see the update. Makes sense now. $\endgroup$
    – lericr
    Sep 5, 2022 at 15:43
  • $\begingroup$ You could also apply Diagonal to your result to extract the correct entries $\endgroup$
    – Lukas Lang
    Sep 5, 2022 at 17:20

5 Answers 5

6
$\begingroup$

What I do if it was n elements in list w and lists a,b,c ?

As an example:

Clear["Global`*"]
a = {1, 2, 3, 5};
b = {2, 1, 3, 6};
c = {1, 3, 2, 8};
amat = {a, b, c};
w = {x + y + z, y, x + z, y - z}; (* e.g. *)
MapThread[ReplaceAll, {w, 
  Thread[{x, y, z} -> #] & /@ Transpose[amat]}]

{4, 1, 5, -2}

$\endgroup$
4
$\begingroup$

Since there's no clear pattern here I suggest the following simple solution based on Part

{a[[1]] + b[[1]] + c[[1]], b[[2]], a[[3]] + c[[3]]}

{4, 1, 5}

$\endgroup$
2
  • $\begingroup$ What I do if it was n elements in list w and lists a,b,c $\endgroup$
    – Ahmed
    Sep 5, 2022 at 15:31
  • $\begingroup$ @Willymith if you had a larger list with larger sublists in the suggested approach here you'd have to do it manually. Note that I already mentioned there's not a clear pattern to follow in order to build the final/resulting list. Also, note that in the other approach you'd still need to build the equivalent of the matrix {{1, 1, 1}, {0, 1, 0}, {1, 0, 1}} by hand in case of larger lists. If there were a clear pattern, we'd have a different approach $\endgroup$
    – bmf
    Sep 5, 2022 at 15:48
3
$\begingroup$

You could use Pick. You want

{x1+y1+z1,y2,x3+z3}

so, let's start by getting things aligned nicely:

Transpose[{x, y, z}]
(* {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}} *)

Now, let's create a new parallel matrix that sets which elements to keep and which to discard. In this case it could be

{{1, 1, 1}, {0, 1, 0}, {1, 0, 1}}

where 1 is keep and 0 is discard. Now we can use Pick:

Pick[Transpose[{x, y, z}], {{1, 1, 1}, {0, 1, 0}, {1, 0, 1}}, 1]
(* {{x1, y1, z1}, {y2}, {x3, z3}} *)

Now just sum each sublist:

Plus @@@ Pick[Transpose[{x, y, z}], {{1, 1, 1}, {0, 1, 0}, {1, 0, 1}}, 1]
(* {x1 + y1 + z1, y2, x3 + z3} *)

Edit

Based on the update, replace x,y,z above with a,b,c

$\endgroup$
3
$\begingroup$

Using Apply, Function and Part (as pointed out @bmf):

Apply[Function[{p1, p2, p3}, {p1[[1]] + p2[[1]] + p3[[1]], p2[[2]], p2[[3]] + p3[[3]]}], {a, b, c}]
(*{4, 1, 5}*)

Just to expand on @lericr's answer:

First, the variables:

varlist = {x, y, z};
l = Length[varlist];
vars = Transpose[
Table[Array[ToExpression[ToString[Part[varlist, i]] <> ToString[#]] &, l], {i, 1, l}]]
(*{{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}*)

Using Association:

mat = {a, b, c};
vars2 = ArrayReshape[Normal[Association[Thread[Flatten[vars] -> Flatten[mat]]]], Dimensions[vars]]

Finally, using Pick as pointed out @lericr:

Map[Composition[Apply[Plus, #] &, #[[All, 2]] &], Pick[Transpose[vars2], Normal[CoefficientArrays[{x + y + z, y, x + z}]][[2]], 1]]
(*{4, 1, 5}*)
$\endgroup$
2
  • 1
    $\begingroup$ Glad to see the 10-ways team up and running!!! Still in the middle of moving, hopefully I'll be fully operational in a month -give or take something :) $\endgroup$
    – bmf
    Sep 5, 2022 at 19:28
  • 1
    $\begingroup$ I'm also glad to see 10-ways team up and running. My best wishes for your move to end well. Greetings :) $\endgroup$ Sep 5, 2022 at 19:39
2
$\begingroup$
a = {1, 2, 3};
b = {2, 1, 3};
c = {1, 3, 2};

w = {x + y + z, y, x + z};

Using ReplacePart

With[{r = Thread[Variables[w] -> #] & /@ Transpose[{a, b, c}]},
 ReplacePart[r, i_ :> (w[[i]] /. r[[i]])]]

{4, 1, 5}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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