6
$\begingroup$

I have a list of seven elements:

list1={0,10,-20,15,-25,20,-30};

I have also another list of six elements:

div={10,15,20,15,10,5};

I would like to create a list subdividing the values of list1 using values of div list as a reference:

Subdivide[0,10,10]
Subdivide[10,-20,15]
Subdivide[-20,15,20]
Subdivide[15,-25,15]
Subdivide[-25,20,10]
Subdivide[20,-30,5]
$\endgroup$

6 Answers 6

6
$\begingroup$

The answer you want can be reached this way:

list1=Partition[list1,2,1]
f=MapThread[Insert,{list1,div,Table[3,{Length[list1]}]}];
Apply[Subdivide,f[[#]]]&/@Range[Length[list1]]
$\endgroup$
1
  • $\begingroup$ Or MapThread[subdivide[First@#1, Last@#1, #2] &, {Partition[list1, 2, 1], div}] $\endgroup$
    – bienti
    Oct 25, 2016 at 13:37
11
$\begingroup$
Subdivide @@@ Partition[Flatten@Riffle[Partition[list1, 2, 1], div], 3]

or

Subdivide @@@ Flatten /@ Transpose[{Partition[list1, 2, 1], div}]

or

Subdivide @@@ Transpose@{list1[[;; -2]], list1[[2 ;;]], div}

or

Subdivide @@@ Transpose@{Most@list1, Rest@list1, div}

Can it be done shorter....

$\endgroup$
8
$\begingroup$

Another way:

MapThread[Subdivide, {Most@list1, Rest@list1, div}]
$\endgroup$
1
  • $\begingroup$ My fav so far, avoids the Transpose. $\endgroup$ Oct 25, 2016 at 17:27
5
$\begingroup$

Quite short:

Subdivide @@@ MapThread[Insert[##, 3] &, {Partition[list1, 2, 1], div}]

or crudely

Table[Subdivide[list1[[i]], list1[[i + 1]], div[[i]]], {i, 1, Length @ div}]
$\endgroup$
4
$\begingroup$

Another simple solution:

Most[list1] + Differences[list1] Map[Subdivide, div]
$\endgroup$
3
$\begingroup$

Also:

Thread@Subdivide[Most@list1, Rest@list1, div]

You can also use Range (which is Listable and threads over its arguments) instead of Subdivide.

Range[Most@#, Rest@#, Differences@#/#2] &[list1, div]

{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{10, 8, 6, 4, 2, 0, -2, -4, -6, -8, -10, -12, -14, -16, -18, -20},
{-20, -(73/4), -(33/2), -(59/4), -13, -(45/4), -(19/2), -(31/4), -6, -(17/4), -(5/ 2), -(3/4), 1, 11/4, 9/2, 25/4, 8, 39/4, 23/2, 53/4, 15},
{15, 37/3, 29/3, 7, 13/3, 5/3, -1, -(11/3), -(19/3), -9, -(35/3), -(43/3), -17, -(59/3), -(67/ 3), -25},
{-25, -(41/2), -16, -(23/2), -7, -(5/2), 2, 13/2, 11, 31/ 2, 20}, {20, 10, 0, -10, -20, -30}}

$\endgroup$

Your Answer

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

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