2
$\begingroup$

I have a list "{{x1,y1},{x2,y2},{x3,y3},{x4,y4},{x5,y5},....,{xn,yn}}" and now want to modify it as below

"{{x1,y1},{(3x1+x2)/4,(3y1+y2)/4},{(x1+x2)/2,(y1+y2)/2},{(x1+3x2)/4,(y1+3y2)/4},{x2,y2},.....,{xn,yn}".

For example: {{1,2},{3,4},{5,6}}. I want this list to be modified as {{1,2},{1.5,2.5},{2,3},{2.5,3.5},{3,4},{3.5,4.5},{4,5},{4.5,5.5},{5,6}}.

Adding two more data points in between parent data points by averaging them. It helps to get more data points on the list. Please help.

$\endgroup$
1
  • $\begingroup$ You are adding three data points between parent points rather than two. $\endgroup$
    – Bob Hanlon
    Apr 2 at 15:59
5
$\begingroup$

Try TimeSeriesResample:

data = {{1, 2}, {3, 4}, {5, 6}}
withIndices = Transpose[{Range[Length[data]], data}];
TimeSeriesResample[withIndices, 1/4, 
  ResamplingMethod -> {"Interpolation", 
    InterpolationOrder -> 1}][[All, 2]]

(* {{1, 2}, {3/2, 5/2}, {2, 3}, {5/2, 7/2}, {3, 4}, {7/2, 9/2},
    {4, 5}, {9/2, 11/2}, {5, 6}} *)
$\endgroup$
5
$\begingroup$

Using Interpolation:

data = {{1, 2}, {3, 4}, {5, 6}};
J = Interpolation[data, InterpolationOrder -> 1];

Table[{x, J[x]}, {x, 1, 5, 1/2}] // N
(*    {{1., 2.}, {1.5, 2.5}, {2., 3.}, {2.5, 3.5}, {3., 4.},
       {3.5, 4.5}, {4., 5.}, {4.5, 5.5}, {5., 6.}}              *)

By changing the InterpolationOrder option you can play with the way the interpolation is done.

$\endgroup$
1
  • $\begingroup$ @flinty, Bob Hanlon and, Roman, Thanks very much. All works great. $\endgroup$ Apr 2 at 18:42
3
$\begingroup$
list = {{1, 2}, {3, 4}, {5, 6}};

Using Subdivide

DeleteDuplicates@Flatten[Subdivide[##, 4] & @@@ Partition[list, 2, 1], 1]

(* {{1, 2}, {3/2, 5/2}, {2, 3}, {5/2, 7/2}, {3, 4}, {7/2, 9/2}, {4, 5}, 
  {9/2, 11/2}, {5, 6}} *)

% /. r_Rational :> N[r]

(* {{1, 2}, {1.5, 2.5}, {2, 3}, {2.5, 3.5}, {3, 4}, {3.5, 4.5}, {4, 5}, 
  {4.5, 5.5}, {5, 6}} *)
$\endgroup$
2
$\begingroup$

A variation on BobHanlon's idea: using Subdivide with BlockMap:

ClearAll[f]

f = Prepend[First @ #] @ BlockMap[Apply[##2 & @@ Subdivide[##, 4] &], #, 2, 1] &

f @ data
{{1, 2}, {3/2, 5/2}, {2, 3}, {5/2, 7/2}, {3, 4}, {7/2, 9/2}, 
 {4, 5}, {9/2, 11/2}, {5, 6}}

Alternatively, Subdivide + MapThread:

ClearAll[f2]

f2 = Prepend[First@ #] @ MapThread[##2 & @@ Subdivide[##, 4] &, {Most @ #, Rest @ #}] &

f2 @ data
{{1, 2}, {3/2, 5/2}, {2, 3}, {5/2, 7/2}, {3, 4}, {7/2, 9/2}, 
 {4, 5}, {9/2, 11/2}, {5, 6}}
$\endgroup$

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.