MATLAB, I want to fill a matrix of 15 columns with several fixed selected values ​​to cover all combinations

Example. I want to populate a 15-column matrix with values ​​of 0.25,0,25,0,25,0,25,0,0,0 ..... (11 columns with zeros) so that all possible combinations of columns are filled. In this example, I use 4 columns with 0.25 and the other 11 col with 0.

The result should look like this: -

0.25  0.25  0.25  0.25  0.00  0.00  0.00  0.00 .......
0.25  0.25  0.25  0.00  0.25  0.00  0.00  0.00 .......
0.25  0.25  0.25  0.00  0.00  0.25  0.00  0.00 .......

 .     .     .     .     .     .     .     .   .......
 .     .     .     .     .     .     .     .   .......
0.25  0.25  0.00  0.25  0.25  0.00  0.00  0.00 .......
0.25  0.25  0.00  0.25  0.00  0.25  0.00  0.00 ....... etc, etc.  

When using "perms" (limited to 10 elements), it processes each "0" as if they were unique, so I get a few lines that are the same. The "unique" function works fine if it is less than 10, but I need to use more. Appreciate any help, thanks

+4
source share
2

accumarray . - .

, , nchoosek

x = nchoosek(1:6, 2);

y = repmat((1:size(x,1))', 1, 2);

, accumarray

z = accummarray([y(:), x(:)], 0.5);

>> z
z =
    0.5000    0.5000         0         0         0         0
    0.5000         0    0.5000         0         0         0
    0.5000         0         0    0.5000         0         0
    0.5000         0         0         0    0.5000         0
    0.5000         0         0         0         0    0.5000
         0    0.5000    0.5000         0         0         0
         0    0.5000         0    0.5000         0         0
         0    0.5000         0         0    0.5000         0
         0    0.5000         0         0         0    0.5000
         0         0    0.5000    0.5000         0         0
         0         0    0.5000         0    0.5000         0
         0         0    0.5000         0         0    0.5000
         0         0         0    0.5000    0.5000         0
         0         0         0    0.5000         0    0.5000
         0         0         0         0    0.5000    0.5000

,

function z = combinations(N, K)

    x = nchoosek(N, K);
    y = repmat((1:size(x,1))', 1, K);
    z = accumarray([y(:), x(:)], 1);

end
+4

combn .

:

c      = combn([0 0.25],15);  
ind    = find(sum(c')==1);
Result = c(ind,:)

, , , , : ?

0 0.25 15 ( 2 ^ 15 ), , [2 ^ 15 x 15 ] (c). c 1, , 0.25 . c VoilΓ !

c ( combn), :

V = [0 0.25];
a = [0:2^15-1]+(1/2) ;
b = [2.^(1-15:0)] ;
id = rem( floor((a(:) * b(:)')) , 2 ) + 1 ;
c = V(id) ; 
+1

All Articles