Message Boards Message Boards

0
|
5052 Views
|
5 Replies
|
5 Total Likes
View groups...
Share
Share this post:

How to create this list?

Hello, I am new to Mathematica and I want to create the following list. Suppose that each entry of a N-dimensional vector can be 0 or 1. How do I obtain all possible N-dimensional vectors where each entry could be 0 or 1? I was able to write the following code but it only works for small N.

l[1] = {0, 1};
l[n_] := l[n] = Flatten[{0, 1, l[n - 1]}, n]
A[n_] := DeleteDuplicates[Subsets[Flatten[{l[n], l[n]}], {n}]]
A[2]
(*{{0, 1}, {0, 0}, {1, 0}, {1, 1}}*)

Kind regards

POSTED BY: Diogo Duarte
5 Replies

Hi,

try

Tuples[{0, 1}, 2]
(*{{0, 0}, {0, 1}, {1, 0}, {1, 1}}*)

If you change 2 to any N, it should do what you want.

Cheers,

M.

POSTED BY: Marco Thiel

Hi Marco, Thank you so much for your reply. I was not aware of this function. Many thanks again.

POSTED BY: Diogo Duarte

I just notice that even Tuples breaks down for large N. If we take N = 100, we have the following error

Tuples::toomany: The length of the output of Tuples[{0,1},100] should be a machine integer. >>

Is there a way to go around it?

POSTED BY: Diogo Duarte

Hi,

I don't think that N=100 is feasible. There are $2^{100}$ different tuples of dimension 100 consisting of entries one and zero. That's a rather large number.

2^100

(*1267650600228229401496703205376*)

That is a huge number. One entry such as $\{0,0\}$ requires 88 Bytes.

ByteCount[{0, 0}]
(*88*)

If we multiply that by $2^{100}$ we get a really large number. Rough estimate (might be wrong) is that you need about $10^{19}$ 1 TB hard drives to store that amount of data.

Even lower dimensions like N=30 cause problems.

Best wishes,

Marco

POSTED BY: Marco Thiel

Hi,

BTW see also this discussion:

http://mathematica.stackexchange.com/questions/46304/generate-a-list-of-values-with-equal-number-of-each-value

The \$MaxMachineInteger on my computer is:

Developer`$MaxMachineInteger
(*9223372036854775807*)

which corresponds to

Log[2, Developer`$MaxMachineInteger] // N
(*63.*)

Notably,

Tuples[{0,1},63]
(*Tuples::toomany: The length of the output of Tuples[{0,1},63] should be a machine integer. >>*)

Tuples[{0,1},62]
(*General::nomem: The current computation was aborted because there was insufficient memory available to complete the computation.*)

So there is a system issue, i.e. address the array, and also there is a memory issue.

Cheers,

M.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract