4
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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) $\endgroup$ Commented Jun 27, 2018 at 18:22
  • 2
    $\begingroup$ 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. $\endgroup$
    – rhermans
    Commented Jun 27, 2018 at 19:18

3 Answers 3

1
$\begingroup$

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 *)

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


(* Out: 
{
 {{1, 1}, {1, 2}, {1, 3}, {1, 4}}, 
 {{2, 5}, {2, 6}}, 
 {{3, 7}, {3, 8}, {3, 9}}
}
*) 
$\endgroup$
1
  • $\begingroup$ Thank you very much. I tried your solutions to my data sets and it worked perfectly! $\endgroup$ Commented Jun 27, 2018 at 18:49
2
$\begingroup$

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

enter image description here

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[
    Apply[Thread[{##}] &, 
      Thread[{list2, PadRight[#, mx, Missing[]] & /@ #}], 1]]
     ] &@list1

enter image description here

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

$\endgroup$
1
$\begingroup$
Inner[
 Thread@*List
 , list2
 , list1
 , List
 ]

or

Map[
 Thread,
 Transpose[{list2, list1}]
 ]

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

$\endgroup$
1
  • $\begingroup$ Thank you! I also tried your answer to my data sets and got what I have expected. $\endgroup$ Commented Jun 27, 2018 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.