0
$\begingroup$

I need to have a vector with the length of 15000. The first 4000 elements have value 12, the next 3000 elements, that is 4001 to 7000 be 0 and the rest of it to the last be 5. I have written it with the following:

Tl = ConstantArray[0, 15000];
Tl[[1 ;; 4000]] = 12;
Tl[[4001 ;; 7000]] = 0;
Tl[[7001 ;; 15000]] = 5;

But I wanna know is there a way to do it with Table or Piecewise or any other ways except part specification. Thanks a bunch.

$\endgroup$
0
$\begingroup$
 Table[Piecewise[{{12, x <= 4000}, {0, x <= 7000}, {5, True}}], {x, 1, 15000}]

The last True means some as "otherwise"

$\endgroup$
2
$\begingroup$

Why not use Join:

r1=Join[
    ConstantArray[12, 4000],
    ConstantArray[0, 3000],
    ConstantArray[5, 8000]
];
$\endgroup$

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.