# List Manipulation - TemporalData on Uneven List

I would like ask you a question about using TemporalData on an uneven list. For example, suppose I have following lists:

list1 = {{1, 2, 3, 4}, {5, 6}, {7, 8, 9}};

list2 = {1, 2, 3};


I want to match each component in list1 to components in list2. My ultimate goal is to make a ListPlot with large data sets. In other words, y = {1, 2, 3, 4} at x = 1,y = {5, 6} at x = 2 and y = {7, 8, 9} at x = 3. I tried Transpose and TemporalData, but it turns out that these commands cannot handle uneven lists. Could you give me any advice? Thanks

• I think you can't avoid balancing the sublists in list1; after doing so, either approach listed in the question can lead to the desired result (at least as far as the example in the question is concerned) Jun 27, 2018 at 18:22
• There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please do your part. Jun 27, 2018 at 19:18

If I understand what you seek, either one of the following should work (using your list definitions):

MapThread[Distribute[{#1, #2}, List] &, {list2, list1}]

(* or *)

(* Out:
{
{{1, 1}, {1, 2}, {1, 3}, {1, 4}},
{{2, 5}, {2, 6}},
{{3, 7}, {3, 8}, {3, 9}}
}
*)

• Thank you very much. I tried your solutions to my data sets and it worked perfectly! Jun 27, 2018 at 18:49

This is how TemporalData can be used to solve the problem:

ListLinePlot[#,
Mesh -> All, Frame -> True, PlotLegends -> Automatic]&@With[{mx = Max[Length /@ #]},
Quiet@TemporalData[
Transpose[PadRight[#, mx, Missing[]] & /@ #], {list2},
MissingDataMethod -> {"Interpolation", InterpolationOrder -> 0}]
] &@list1


Evaluating the code above produces

There are 4 paths in the TemporalData object; the first two contain three observations ({1, 5, 7} and {2, 6, 8} respectively) while the last two contain varying number of numerical observations each (namely {3, Missing[], 9} and {4, Missing[], Missing[]} respectively).

The Missing[] entries get replaced by the previous numerical value in each path, courtesy of the value of the MisingDataMethod supplied to the TemporalData object.

Using Transpose (in a broad sense) can produce the same result

ListLinePlot[#,
Mesh -> All, Frame -> True, PlotLegends -> Automatic] &@With[{mx = Max[Length /@ #]},
Transpose[
] &@list1


(this time there is no interpolation for Missing[] values)

Inner[
, list2
, list1
, List
]


or

Map[

in case you want the values grouped by abscissa value. Otherwise, wrap either function with Flatten[ ... ,1].