11
$\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$

9 Answers 9

9
$\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
  • 1
    $\begingroup$ Or MapThread[subdivide[First@#1, Last@#1, #2] &, {Partition[list1, 2, 1], div}] $\endgroup$
    – bienti
    Commented Oct 25, 2016 at 13:37
14
$\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$
11
$\begingroup$

Another way:

MapThread[Subdivide, {Most@list1, Rest@list1, div}]
$\endgroup$
1
  • $\begingroup$ My fav so far, avoids the Transpose. $\endgroup$ Commented Oct 25, 2016 at 17:27
7
$\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$
6
$\begingroup$

Another simple solution:

Most[list1] + Differences[list1] Map[Subdivide, div]
$\endgroup$
5
$\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$
5
$\begingroup$
list1 = {0, 10, -20, 15, -25, 20, -30};

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

Using SequenceCases

Subdivide @@@ Append @@@
  Transpose[{SequenceCases[list1, {_, _}, Overlaps -> True], div}]

returns

{{  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$
5
$\begingroup$
list1 = {0, 10, -20, 15, -25, 20, -30};

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

Using MovingMap:

Inactive[Subdivide] @@@ Append @@@ Thread[{MovingMap[# &, list1, 1], div}]

enter image description here

Finally, use Activate[%] command to get result.

$\endgroup$
4
$\begingroup$
list1 = {0, 10, -20, 15, -25, 20, -30};
div = {10, 15, 20, 15, 10, 5};

MapThread[Join, {Partition[list1, 2, 1], List /@ div}] // 
 Map[Apply[Inactive[Subdivide]]]

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

enter image description here

Issue Activate[%] command to get result.

$\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.