matlab - How to generate a matrix of vector combinations with zeros for excluded elements? -
i want create matrix combinations of elements of 1 vector fulfill condition
for example, have vector
a = [1 2 3 4 5]
and want create matrix
a = [1 0 0 0 0; 1 2 0 0 0; 1 2 3 0 0; 1 2 3 4 0; 1 2 3 4 5; 0 2 0 0 0; 0 2 3 0 0; ........;]
and rows fulfill condition can use command:
b = sum(a')' > value
but don't know how generate matrix
you can generate possible binary combinations determine matrix want:
a = [1 2 3]; n = size(a,2); % generate bit combinations c =(dec2bin(0:(2^n)-1)=='1'); % remove first line c = c(2:end,:) n_c = size(c,1); a_rep = repmat(a,n_c,1); result = c .* a_rep
output:
c = 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 result = 0 0 3 0 2 0 0 2 3 1 0 0 1 0 3 1 2 0 1 2 3
Comments
Post a Comment