Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to initialize this kind of vector : {0,0,1,1,0,1,0,1,1,1,1,1} with Table.
I tried Table[{x, y, 1}, {y, 0, 1, 1}, {x, 0, 1, 1}] but it outputs {{0,0,1},{1,0,1},{0,1,1},{1,1,1}}.
What is the right method to correctly initialize my vector without creating sub-lists ?

share|improve this question

2 Answers 2

up vote 5 down vote accepted
Table[{x, y, 1}, {y, 0, 1, 1}, {x, 0, 1, 1}] // Flatten

gives

{0,0,1,1,0,1,0,1,1,1,1,1}
share|improve this answer

Consider also:

Flatten @ Array[{#2, #, 1} &, {2, 2}, 0]
{0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1}

This is faster than Table, if applicable:

n = 2200;

Table[{x, y, 1}, {y, 0, n}, {x, 0, n}] // Flatten // Length // Timing
Array[{#2, #, 1} &, {n, n} + 1, 0]     // Flatten // Length // Timing
{1.435209, 14533203}

{0.280802, 14533203}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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