how to permute all combinations of this vector??

hello i'm struggling to write code for this problem; I want to display all possible combinations of the vectors of the form (1,b1,b2,b3,.....bN),where bi∈{0,1}
so the first entry of the vector is always 1 and the rest take the value 0 or 1. I then want to print every combination of this. How would you go about coding this ?? thanks for your help in advance

2 Commenti

How large is N?
You should recognize that for large N, the solution might require billions of permutations.
Essentially, you can think of the result as the binary representations of all integers of the form
2^(N+1) + K
where K is any integer < 2^N with exactly sum(B) non-zero bits. I'm not sure that really helps a lot.
yeah thanks quite small around 22. Im plotting combinations stock price data following a binomial tree, 1 for up movement, 0 for down movement and over n days being number of trading days

Accedi per commentare.

 Risposta accettata

This is one approach:
N = 4 ;
[b{1:N}] = ndgrid([0 1])
c = reshape(cat(N+1, ones(size(b{1})), b{:}), [], N+1)

3 Commenti

Hmm. I initially interpreted that differently, but I think you are right. But if you are correct in your assessment, then the solution is easier.

N = 4;
dec2bin(2^N + (0:2^(N)-1)) - '0'
ans =
     1     0     0     0     0
     1     0     0     0     1
     1     0     0     1     0
     1     0     0     1     1
     1     0     1     0     0
     1     0     1     0     1
     1     0     1     1     0
     1     0     1     1     1
     1     1     0     0     0
     1     1     0     0     1
     1     1     0     1     0
     1     1     0     1     1
     1     1     1     0     0
     1     1     1     0     1
     1     1     1     1     0
     1     1     1     1     1
Yes, thats excellent exactly what I want to produce
+1 You should make this an answer, John :)

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by