Linear matrix / vector combination

B - matrix [1x8], which can also be considered as two halves:

B = -1 -1 0 0   0 0 1 1

There may be one, two, three or four -1in the first half, and in the second half there should be an equal amount 1. This should be done in linear combinations.

For example, if there are two in the first half -1, they can be placed in 4 choose 2 = 6ways, and for each of them there will be 6 ways to place two 1in the second half. Thus, the system has a total of 6 * 6 = 36 methods. that is, 36 different values ​​for B if there are two in the first half -1.

How can i do this?

+5
source share
2 answers

, .

%# make permutations using dec2bin (start from 17 since it the first solution)
allB = str2double(num2cell(dec2bin(17:255)));

%# change sign in the first half, then check that the total is ok
allB(:,1:4) = - allB(:,1:4);
allB = allB(sum(allB,2)==0,:);

allB B

+5

:

%# generate all possible version of first half
h1 = num2cell(-(dec2bin(1:15)-'0'),2);

%# generate all possible version of second half
h2 = arrayfun(@(i) unique(perms([zeros(1,4-i) ones(1,i)]),'rows'), (1:4)', 'UniformOutput',false);

%'# number of 1s in each row of h1
n = -cellfun(@sum, h1);

%# get final result by combining h1 and h2
B = cellfun(@(a,b) [repmat(a,size(b,1),1) b], h1, h2(n), 'UniformOutput',false);
B = cell2mat(B);

:

B =
     0     0     0    -1     0     0     0     1
     0     0     0    -1     0     0     1     0
     0     0     0    -1     0     1     0     0
     0     0     0    -1     1     0     0     0
     0     0    -1     0     0     0     0     1
     0     0    -1     0     0     0     1     0
     0     0    -1     0     0     1     0     0
     0     0    -1     0     1     0     0     0
     0     0    -1    -1     0     0     1     1
     0     0    -1    -1     0     1     0     1
     0     0    -1    -1     0     1     1     0
     0     0    -1    -1     1     0     0     1
     0     0    -1    -1     1     0     1     0
     0     0    -1    -1     1     1     0     0
     0    -1     0     0     0     0     0     1
     0    -1     0     0     0     0     1     0
     0    -1     0     0     0     1     0     0
     0    -1     0     0     1     0     0     0
     0    -1     0    -1     0     0     1     1
     0    -1     0    -1     0     1     0     1
     0    -1     0    -1     0     1     1     0
     0    -1     0    -1     1     0     0     1
     0    -1     0    -1     1     0     1     0
     0    -1     0    -1     1     1     0     0
     0    -1    -1     0     0     0     1     1
     0    -1    -1     0     0     1     0     1
     0    -1    -1     0     0     1     1     0
     0    -1    -1     0     1     0     0     1
     0    -1    -1     0     1     0     1     0
     0    -1    -1     0     1     1     0     0
     0    -1    -1    -1     0     1     1     1
     0    -1    -1    -1     1     0     1     1
     0    -1    -1    -1     1     1     0     1
     0    -1    -1    -1     1     1     1     0
    -1     0     0     0     0     0     0     1
    -1     0     0     0     0     0     1     0
    -1     0     0     0     0     1     0     0
    -1     0     0     0     1     0     0     0
    -1     0     0    -1     0     0     1     1
    -1     0     0    -1     0     1     0     1
    -1     0     0    -1     0     1     1     0
    -1     0     0    -1     1     0     0     1
    -1     0     0    -1     1     0     1     0
    -1     0     0    -1     1     1     0     0
    -1     0    -1     0     0     0     1     1
    -1     0    -1     0     0     1     0     1
    -1     0    -1     0     0     1     1     0
    -1     0    -1     0     1     0     0     1
    -1     0    -1     0     1     0     1     0
    -1     0    -1     0     1     1     0     0
    -1     0    -1    -1     0     1     1     1
    -1     0    -1    -1     1     0     1     1
    -1     0    -1    -1     1     1     0     1
    -1     0    -1    -1     1     1     1     0
    -1    -1     0     0     0     0     1     1
    -1    -1     0     0     0     1     0     1
    -1    -1     0     0     0     1     1     0
    -1    -1     0     0     1     0     0     1
    -1    -1     0     0     1     0     1     0
    -1    -1     0     0     1     1     0     0
    -1    -1     0    -1     0     1     1     1
    -1    -1     0    -1     1     0     1     1
    -1    -1     0    -1     1     1     0     1
    -1    -1     0    -1     1     1     1     0
    -1    -1    -1     0     0     1     1     1
    -1    -1    -1     0     1     0     1     1
    -1    -1    -1     0     1     1     0     1
    -1    -1    -1     0     1     1     1     0
    -1    -1    -1    -1     1     1     1     1
+2

All Articles