# use Range to form a vector

I have a function

y[k_]:={0,0,0,I*t^k};


and I need to get a vector

Join[y,y,y,y,y,y,y]


and it is just for instance. The real vector I am using is much more complicated, and there are more y[k]s. I realized that manually typing the Join part is inefficient and tiring.

How can I achieve my goal with a short code? Maybe involve Nest or Do. Thank you.

Array[y, 7, 1, Join]


{0, 0, 0, I t, 0, 0, 0, I t^2, 0, 0, 0, I t^3, 0, 0, 0, I t^4, 0, 0, 0, I t^5, 0, 0, 0, I t^6, 0, 0, 0, I t^7}

• Really equisite! Thank you! – Robin_Lyn Jul 6 '18 at 11:05
• @Robin_Lyn, my pleasure. Thank you for the accept. – kglr Jul 6 '18 at 11:05

Try with the following code:

y[k_] := {0, 0, 0, I*t^k};

test1 = Join[y, y, y, y, y, y, y];

test2 = Join @@ y /@ Range

test1 == test2


{0, 0, 0, I t, 0, 0, 0, I t^2, 0, 0, 0, I t^3, 0, 0, 0, I t^4, 0, 0, 0, I t^5, 0, 0, 0, I t^6, 0, 0, 0, I t^7}

True

Instead of Range you can use any list of arguments you need.

EDIT:

comparison between my solution and kglr solution:

RepeatedTiming@Array[y, 10000, 1, Join] // First
RepeatedTiming@(Join @@ y /@ Range) // First


0.018

0.019

Basically the same performance :)

Personally, I'd probably use kglr solution for consecutive values of the argument, mine in case I need more tunability in the argument's values

• Good alternative to kglr's solution. Thank you! People in this forum are really friendly. – Robin_Lyn Jul 6 '18 at 11:07
• You are welcome :) – Fraccalo Jul 6 '18 at 11:10