I guess the algorithm to create the cell "a":
a{1} = [1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0];
a{2} = [1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0];
a{3} = [1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0];
a{4} = [1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0];
If we now concatenate the rows, we get the binary representation of the values 2^N-1 to 0 as columns:
A = cat(1, a{:})
>> A = [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0; ...
1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0; ...
1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0; ...
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
Instead of creating the rows of the cell "a" it is easier to create the matrix directly:
N = 4;
A = (dec2bin(2^N-1:-1:0) - '0').';
If I guess that "b" is a numerical vector of the same size:
b = rand(1, 16);
cp = A * b(:);
[~, idx] = min(cp);
result = A(idx, :);
Best Answer