1
$\begingroup$

I have a list of five elements. For example,

list1 = {a1, a2, a3, a4, a5};

I'm trying to get a list:

{{a2/a1}, {a3/a1, a3/a2}, {a4/a1, a4/a2, a4/a3}, {a5/a1, a5/a2, a5/a3, a5/a4}}

How can I do this? And can I get it without Table?

Then, it is necessary to combine it with another list

list2 = {{b1, c1}, {b2, c2}, {b3, c3}, {b4, c4}, {b5, c5}};

as

{{b1, c1, a2/a1}, {{b1, c1, a3/a1}, {b2, c2, a3/a2}}, {{b1, c1, a4/a1}, {b2, c2, a4/a2}, 
{b3, c3, a4/a3}}, {{b1, c1, a5/a1}, {b2, c2, a5/a2}, {b3, c3, a5/a3}, {b4, c4, a5/a4}}}

I'd like to do this with Mathematica functions. Any ideas?

$\endgroup$
0

4 Answers 4

3
$\begingroup$
step1 = ReplaceList[list1, {a__, b_, ___} :> b/{a}]
step2 = FlattenAt[(Flatten /@ Thread[{list2[[;; Length@#1]], #1}]) & /@step1, 1]
$\endgroup$
4
$\begingroup$
list11=list1[[#]]/list1[[;; # - 1]] & /@ Range[2, Length[list1]]

Fro the second question may be this work:

list22 = list2[[;; # - 1]] & /@ Range[2, Length[list2]]
MapThread[Append, #] & /@ Transpose[{list22, list11}]
$\endgroup$
1
  • $\begingroup$ This was my first thought as well. $\endgroup$ Commented Jul 16, 2015 at 8:44
1
$\begingroup$

The first thing that came to mind:

list1 = {a1, a2, a3, a4, a5};
list2 = {{b1, c1}, {b2, c2}, {b3, c3}, {b4, c4}, {b5, c5}};

Rest@FoldList[Append, {}, Most@list1]

Rest[list1] / %

Flatten /@ Thread @ {Take[list2, Length@#], #} & /@ %       (* not too pretty *)
{{a2/a1}, {a3/a1, a3/a2}, {a4/a1, a4/a2, a4/a3}, {a5/a1, a5/a2, a5/a3, a5/a4}}

{{{b1, c1, a2/a1}}, {{b1, c1, a3/a1}, {b2, c2, a3/a2}}, {{b1, c1, a4/a1},
 {b2, c2, a4/a2}, {b3, c3, a4/a3}}, {{b1, c1, a5/a1}, {b2, c2, a5/a2},
  {b3, c3, a5/a3}, {b4, c4, a5/a4}}}
$\endgroup$
0
$\begingroup$

Ugly but another way (perhaps):

Join @@ With[{r = 
    GatherBy[
     Join @@ MapIndexed[#1/list1[[1 ;; #2[[1]]]] &, Rest@list1], 
     Denominator]},
  MapThread[Map[Function[x, Append[#1, x]], #2] &, {Most@list2, r}]]
$\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.