2
$\begingroup$

I have a small problem that I cannot solve.

I have two tables, one with three elements:

a = {0, L/c, 2L/c};

and the other with 9 elements, all 0:

b = {0, 0, 0, 0, 0, 0, 0, 0, 0};

I would like to put the elements from table a into table b, giving so that I get the following table:

{0, L/c, 2L/c, 0, L/c, 2L/c, 0, L/c, 2L/c}

Can someone please give me some advice?

$\endgroup$
  • 3
    $\begingroup$ Do you need b at all or do you simply want to generate a list of n repeated elements? $\endgroup$ – Yves Klett Jul 1 '13 at 7:03
5
$\begingroup$

I don't know if b is something important, but if it is not:

a = {0, L/c, 2 L/c}; 
PadLeft[a, 9, a]

{0, L/c, (2 L)/c, 0, L/c, (2 L)/c, 0, L/c, (2 L)/c}

and if it is:

b = ConstantArray[0, 9];
PadLeft[a, Length@b, a]

{0, L/c, (2 L)/c, 0, L/c, (2 L)/c, 0, L/c, (2 L)/c}

$\endgroup$
3
$\begingroup$
a = {0, L/c, 2 L/c};
b = ConstantArray[0, 9];
Flatten[ConstantArray[a, Length[b]/Length[a]]]

Or, even more succinctly:

  b = Flatten[ConstantArray[{0, L/c, 2 L/c}, 3]]
$\endgroup$
  • $\begingroup$ Just found out. Thank you very much bill $\endgroup$ – de_wight Jul 1 '13 at 7:04
  • 1
    $\begingroup$ @de_wight, if this answer has solved your problem, you should accept it by clicking on the check mark on the left. $\endgroup$ – m_goldberg Jul 1 '13 at 7:36

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.