MATLAB, I want to fill a 15 column matrix with a few fixed selected values such that all combinations are covered -
example, wish fill 15 column matrix values 0.25,0.25,0.25,0.25,0,0,0 .....( 11 columns zeros) possible column combinations filled. in example using 4 columns 0.25 , other 11 col's 0's.
result should :-
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 10 elements anyway ) treats each "0" if unique hence multiple rows same. "unique" function works fine if less 10 elements need use more that. appreciate assistance, thanks
the function accumarray
friend in case. i'll simpler example - creating 6 column matrix 2 columns filled , rest zero.
first, list of horizontal indices want filled using nchoosek
x = nchoosek(1:6, 2);
now vertical coordinates
y = repmat((1:size(x,1))', 1, 2);
finally, use accumarray
create desired matrix
z = accummarray([y(:), x(:)], 0.5);
the result is
>> 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
packaging function, get
function z = combinations(n, k) x = nchoosek(n, k); y = repmat((1:size(x,1))', 1, k); z = accumarray([y(:), x(:)], 1); end
Comments
Post a Comment