2
$\begingroup$

For demonstrating purposes suppose I have the following.

j[-1, z_] := Cos[z];
j[1, z_] := Sin[z];
h[n_] := Table[j[(-1)^n, i π/2], {i, 5}];

What I want to do is simply combine the result I get from h[1] and h[2]

For h[1] the output is

{0, -1, 0, 1, 0}

While for h[2] the output is

{1, 0, -1, 0, 1}

I want to merge these two lists so I get

{0, -1, 0, 1, 0, 1, 0, -1, 0, 1}

How can I do it? Or is there perhaps an easier more elegant way to do it, like to tell my function h[n_] to do the case n=1 and n=2 straight away?

$\endgroup$

closed as off-topic by Daniel Lichtblau, Yves Klett, WReach, Chris K, Mr.Wizard Oct 5 '17 at 3:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, Yves Klett, WReach, Chris K, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    $\begingroup$ Do you mean to Join them? $\endgroup$ – Szabolcs Oct 4 '17 at 14:23
  • $\begingroup$ I get for your example: h[1]={0.707107, 0., -0.707107, -1., -0.707107} and h[2]={0.707107, 1., 0.707107, 0., -0.707107}? $\endgroup$ – mrz Oct 4 '17 at 14:54
  • $\begingroup$ Yeah Join would have done it as well. @mrz Sorry it should have been π/2 and not π/4. $\endgroup$ – Turbotanten Oct 4 '17 at 15:40
  • $\begingroup$ h[1]~Join~h[2] $\endgroup$ – corey979 Oct 4 '17 at 21:44
1
$\begingroup$

Join will do the trick:

j[-1, z_] := Cos[z];
j[1, z_] := Sin[z];
h[n_] := Table[j[(-1)^n, i π/2], {i, 5}];

Join[h[1], h[2]]

(* {0, -1, 0, 1, 0, 1, 0, -1, 0, 1} *)
$\endgroup$
0
$\begingroup$

Okey I figured it out. Writing the following does the trick

j[-1, z_] := Cos[z];
j[1, z_] := Sin[z];
h = Table[j[(-1)^n, i π/2], {i, 5}, {n, 2}]
$\endgroup$
  • 4
    $\begingroup$ To get the answer that you requested in the question, you need to change the order of summations and use Flatten, i.e., h = Table[j[(-1)^n, i \[Pi]/2], {n, 2}, {i, 5}] // Flatten $\endgroup$ – Bob Hanlon Oct 4 '17 at 16:14
0
$\begingroup$
j[-1, z_] := Cos[z];
j[1, z_] := Sin[z];
h = Table[j[(-1)^n, i π/2], {i, 5}, {n, 2}]

Flatten@{Take[h[[All, 1]]], Take[h[[All, 2]]]}

(* {0,-1,0,1,0,1,0,-1,0,1} *)
$\endgroup$

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