# Extracting Y-coordinates for list1, for X-coordinates of list2 while their lengths are not same

I have two lists

list1={{1,2},{3,4},{5,6},{7,8},{9,10},{11,12}};
list2={{1.3,12},{3.2,14},{5.4,15},{7.1,16},{9.5,17},{11.6,18},{12.7,19.7}};


X coordinates from both lists represent the same parameter and they are slightly off, while the Y coordinates are from different parameters. The lengths of lists are not the same. I would like to have Y coordinate values in list1 for X coordinates of list2. Now list1 has to look like {{1.3,Y of list1 = ?},{3.2,?},{5.4,?},{7.1,?},{9.5,?},{11.6,?},{12.7,?}}; It would be highly appreciated if someone helps with this for even larger data sets. Thanks very much for your time

Construct an interpolating function from list1 and evaluate it at x values of list2:

iF = Interpolation[list1,
"ExtrapolationHandler" -> {Automatic, "WarningMessage" -> False}];

interpolatedlst1 = {#, iF@#} & /@ list2[[All, 1]]

{{1.3, 2.3}, {3.2, 4.2}, {5.4, 6.4}, {7.1, 8.1}, {9.5, 10.5},
{11.6, 12.6}, {12.7, 13.7}}


Alternatively, use TimeSeriesResample with first column of list2 as the second argument:

interpolatedlst1b = Quiet@TimeSeriesResample[list1, {list2[[All, 1]]},
ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}]

{{1.3, 2.3}, {3.2, 4.2}, {5.4, 6.4}, {7.1, 8.1}, {9.5, 10.5},
{11.6, 12.6}, {12.7, 13.7}}

ListPlot[{list1, list2, interpolated2},
Mesh -> All,
Epilog -> {ColorData[97]@3, Dashed, Line @ interpolatedlst1},
PlotRange -> All]


• @ kglr: Thanks very much. Your code is very useful Commented Apr 5, 2021 at 20:51