Genrate all combinations of a vector
2 views (last 30 days)
Show older comments
I have a vector of size n, whose each element can be an integer in the range from 0 to k. I need to generate all possible variants of such vector, i.e. k^n vectors totally.
The solutions I have found haven't met my need:
- allcomb.m doesn't work with n>=100. I got the following error
Maximum variable size allowed by the program is exceeded.
Error in ndgrid (line 66)
x = reshape(x(:,ones(1,prod(s))),[length(x) s]); % Expand x
- perms only generates possible permutations of the n elements of v.
I wonder if there is any function in matlab that does what I need?
Thanks,
5 Comments
Walter Roberson
on 30 Nov 2011
If you used one byte per element of the vector (e.g., uint8 data type), then you would have n bytes per entry, and (k+1)^n entries, for a total of approximately n*(k+1)^n bytes.
For n=100 and k=3, that would be 100*4^100 which would be about 1.6E62 bytes.
1 gigabyte is approximately 10^9 bytes, so you are asking for approximately 1.6E53 gigabytes.
So how much can you realistically store in (say) 4 gigabytes (the maximum address space for a 32 bit process)? n*4^n = 2^32 solves down to n = (1/2)*LambertW(8589934592*ln(2))/ln(2) which is a little over 14.
Thus, you realistically cannot produce all the combinations for vectors of length greater than 14.
Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!