I'm trying to use nchoosek(v,k)
to get all possible combinations of four items of value one distributed randomly over 4 positions. My goal is to get this:
[4 0 0 0;
0 4 0 0;
0 0 4 0;
0 0 0 4;
3 1 0 0;
3 0 1 0;
3 0 0 1;
1 3 0 0;
0 3 1 0;
0 3 0 1;
1 0 3 0;
0 1 3 0;
0 0 3 1;
1 0 0 3;
0 1 0 3;
0 0 1 3;
2 2 0 0;
2 0 2 0;
2 0 0 2;
0 2 2 0;
0 2 0 2;
0 0 2 2;
2 1 1 0;
2 1 0 1;
2 0 1 1;
1 2 1 0;
1 2 0 1;
0 2 1 1;
1 1 2 0;
1 0 2 1;
0 1 2 1;
1 1 0 2;
1 0 1 2;
0 1 1 2;
1 1 1 1];
However I'm not sure how to input v
and k
correctly to achieve this with nchoosek()
. Whatever I put into v
, I also get as an output.
nchoosek([1,1,1,1], 4)
[1,1,1,1]
nchoosek([1,0,0,0], 4)
[1,0,0,0]
nchoosek([4,0,0,0], 4)
[4,0,0,0]
nchoosek([1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1], 4)
Error using nchoosek (line 29)
The first argument has to be a scalar or a vector.
In short I don't get the input and output of nchoosek(v,k)
. Anybody know how to do this? I looked up some questions here but they didn't help me along.