I have two 2d lists:
list1 = Transpose[{{1, 4, 7, 9, 4, 6, 7, 8, 3, 2},
{0.12, 0.19, 0.29, 0.39, 0.51, 0.62, 0.71, 0.80, 0.89, 0.99}}];
(*{{1, 0.12},
{4, 0.19},
{7, 0.29},
{9, 0.39},
{4, 0.51},
{6, 0.62},
{7, 0.71},
{8, 0.8},
{3, 0.89},
{2, 0.99}}*)
list2 = Transpose[{{3, 6, 9, 2, 4, 8, 5, 7, 4, 8},
{0.30, 0.40, 0.51, 0.62, 0.72, 0.79, 0.88, 0.98, 1.09, 1.2}}];
(*{{3, 0.3},
{6, 0.4},
{9, 0.51},
{2, 0.62},
{4, 0.72},
{8, 0.79},
{5, 0.88},
{7, 0.98},
{4, 1.09},
{8, 1.2}}*)
I want to synchronize the two lists according to their second vector content.
The numbers in the second vector are in both lists increasing; the difference between two neighboured values is nearly "constant" in list1[[All,2]]
and list2[[All,2]]
, but slightly varying.
My code:
n = Flatten@Nearest[list1[[All, 2]], list2[[All, 2]]]
(*{0.29, 0.39, 0.51, 0.62, 0.71, 0.8, 0.89, 0.99, 0.99, 0.99}*)
p1 = DeleteDuplicates@Flatten[Position[list1[[All, 2]], #] & /@ n]
(*{3, 4, 5, 6, 7, 8, 9, 10}*)
result1 = list1[[p1]]
(*{{7, 0.29}, {9, 0.39}, {4, 0.51}, {6, 0.62}, {7, 0.71}, {8, 0.8}, {3, 0.89}, {2, 0.99}}*)
result2 = list2[[p1 - Min[p1] + 1]]
{{3, 0.3}, {6, 0.4}, {9, 0.51}, {2, 0.62}, {4, 0.72}, {8, 0.79}, {5, 0.88}, {7, 0.98}}
result = Transpose[{result1, result2}]
(*{{{7, 0.29}, {3, 0.3}},
{{9, 0.39}, {6, 0.4}},
{{4, 0.51}, {9, 0.51}},
{{6, 0.62}, {2, 0.62}},
{{7, 0.71}, {4, 0.72}},
{{8, 0.8}, {8, 0.79}},
{{3, 0.89}, {5, 0.88}},
{{2, 0.99}, {7, 0.98}}}*)
Do you have another solution for this?
list1 = Transpose[{{1, 4, 7, 9, 10, 4, 6, 7, 8, 3, 2}, {0.12, 0.19, 0.29, 0.39, 0.39, 0.51, 0.62, 0.71, 0.80, 0.89, 0.99}}]
andlist2 = Transpose[{{3, 6, 9, 10, 2, 4, 8, 5, 7, 4, 8}, {0.30, 0.40, 0.40, 0.51, 0.62, 0.72, 0.79, 0.88, 0.98, 1.09, 1.2}}]
then the current 4 answer and your code yields 5 different results! Which is correct? $\endgroup$