TransWikia.com

Fix two data lists intertwined

Mathematica Asked on October 22, 2021

I have a long stream of mixed up data, but to simplify let us say I have these two lists (Actually I have 4 lists with 600 elements each):

l1 = {{0, 0}, {1, 1}, {2, 4}, {3, 9}, {4, 
   16}, {5, -12.5}, {6, -14.4}, {7, -14.7}, {8, -12.8}, {9, -8.1}, {10, 0}, {11, 12.1}, {12, 28.8}};

l2 = {{0, 0.}, {1, -0.9}, {2, -3.2}, {3, -6.3}, {4, -9.6}, {5,25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100}, {11, 121}, {12,144}};

If you ListPlot them you cannot see the problem, but if you ListLinePlot them it becomes clear. How do I un-interwind something like this?

Show[ListPlot[l1], ListPlot[l2], PlotRange -> All]
Show[ListLinePlot[l1], ListLinePlot[l2], PlotRange -> All]

enter image description here

2 Answers

ClearAll[unTangle]
unTangle = Module[{l = #}, 
    l[[All, All, 2]] = Transpose[Sort /@ Transpose[l[[All, All, 2]]]];l] &;

Examples:

Row[{ListLinePlot[{l1, l2}, ImageSize -> Medium], 
  ListLinePlot[unTangle @ {l1, l2}, ImageSize -> Medium]}, Spacer[10]]

enter image description here

SeedRandom[1]
lsts = MapIndexed[{#2[[1]], #} &] /@ RandomReal[1, {5, 50}];

Row[{ListLinePlot[lsts, ImageSize -> Medium], 
  ListLinePlot[unTangle @ lsts, ImageSize -> Medium]}, Spacer[10]]

enter image description here

Alternatively,

ClearAll[unIntertwine]
unIntertwine = TemporalData[Transpose[Sort /@ Transpose[#[[All, All, 2]]]], 
  {#[[1, All, 1]]}] &;

Normal[unIntertwine@{l1, l2}] == unTangle @ {l1, l2}
True
Normal[unIntertwine @ lsts] == unTangle @ lsts 
True

Answered by kglr on October 22, 2021

ListLinePlot[{l1, l2}]

enter image description here

l0 = Transpose@{l1[[All, 2]], l2[[All, 2]]};
ListLinePlot[{Max /@ l0, Min /@ l0}, DataRange -> {0, 13}]

enter image description here

Answered by OkkesDulgerci on October 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP