0
$\begingroup$

This is a contrived example, but it gets the point across. I am looking to use the following as inputs:

a = {10, 11};
b = {5, 7};
MakeCD[x_] := {{x, 2 x}, {3 x, 4 x}};

to function that takes a list of sublists. ListPlot is a suitable example of the function.

Using 1 as the argument to MakeCD, MakeCD returns: {{1, 2}, {3, 4}}

I am looking to supply {{10, 11}, {5, 7}, {1, 2}, {3, 4}} to ListPlot

I can achieve this with the following:

{c, d} = MakeCD[1];
{a, b, c, d}

Also with:

Join[{a}, {b}, MakeCD[1]]

I am sure there is a better way to do this, but I have not yet stumbled upon it yet.

$\endgroup$
2
  • $\begingroup$ Something like Flatten[{MakeCD[1], MakeCD[10]}, 1] ? $\endgroup$ Aug 30 '13 at 20:43
  • $\begingroup$ @b.gatessucks unfortunate contrived example that MakeCD can generate a,b,c,d. I changes that. $\endgroup$
    – mmorris
    Aug 30 '13 at 21:33
3
$\begingroup$

Second try:

From the comments I believe you are looking for Sequence:

{a, b, Sequence @@ MakeCD[1]}
{{10, 11}, {5, 7}, {1, 2}, {3, 4}}

You can include this in the definition of MakeCD, or an auxiliary function, to streamline this:

MakeCD2[x_] := Sequence[{x, 2 x}, {3 x, 4 x}];

{a, b, MakeCD2[1]}
{{10, 11}, {5, 7}, {1, 2}, {3, 4}}

You can also use SlotSequence to perform a similar action:

{a, b, ##}& @@ MakeCD[1]
{{10, 11}, {5, 7}, {1, 2}, {3, 4}}

Or more in line with your application:

plotfunction[{a, b, ##}, plotoptions] & @@ MakeCD[1]
plotfunction[{{10, 11}, {5, 7}, {1, 2}, {3, 4}}, plotoptions]

For an advanced method that will be useful with held expressions see:
Injecting a sequence of expressions into a held expression

$\endgroup$
5
  • $\begingroup$ I have a variable number of series of date data to be plotted by DateListPlot. In my example, I have two represented by a & b. There could be any number of series of date data (1-5+). In addition to the series data, I will also always have two additional series of date date that are generated by a function. In my example, that is represented by MakeCD[]. The problem is that any function that returns multiple values, returns a list of the values, in my case series of date data. I was hoping for a way to remove the outer list and just supply the two sublists to DateListPlot $\endgroup$
    – mmorris
    Aug 30 '13 at 22:24
  • $\begingroup$ i.e. DateListPlot[{a,b,x,y,z, Delistify[MakeCD[1]] Obviously Delistify[] does not exist, but the result would have been DateListPlot[{a,b,x,y,z,{1,2},{3,4}] And yes that would not have plotted since {1,2} should have been something like {{{2013, 8, 27}, 42}, {{2013, 8, 28}, 40}}. Just trying to save space, but probably cause more confusion. $\endgroup$
    – mmorris
    Aug 30 '13 at 22:31
  • $\begingroup$ @mmorris Oh! I think you're just looking for Sequence. Update imminent. $\endgroup$
    – Mr.Wizard
    Aug 30 '13 at 22:42
  • $\begingroup$ @mmorris Don't miss my addition regarding SlotSequence -- it's the method I use most often in cases like this. $\endgroup$
    – Mr.Wizard
    Aug 30 '13 at 22:48
  • $\begingroup$ I understand Sequence, wrapping my head around SlotSequence. Thanks $\endgroup$
    – mmorris
    Aug 30 '13 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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