在MATLAB中寻找互补向量的快捷方法

lyn*_*lyn 5 algorithm matlab combinations

我有一N排二进制向量矩阵,即

mymatrix = [ 1 0 0 1 0;
             1 1 0 0 1;
             0 1 1 0 1;
             0 1 0 0 1;
             0 0 1 0 0;
             0 0 1 1 0;
             ....       ]
Run Code Online (Sandbox Code Playgroud)

在哪里我想找到行的组合,当它们加在一起时,我会完全得到:

[1 1 1 1 1]
Run Code Online (Sandbox Code Playgroud)

所以在上面的例子中,将工作的组合1/3,1/4/52/6.

我现在的代码是:

i = 1;
for j = 1:5
    C = combnk([1:N],j); % Get every possible combination of rows
    for c = 1:size(C,1)
        if isequal(ones(1,5),sum(mymatrix(C(c,:),:)))
            combis{i} = C(c,:);
            i = i+1;
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

但正如你想象的那样,这需要一段时间,特别是因为combnk在那里.

什么可能是一个有用的算法/功能,可以帮助我加快速度?

Tom*_*zzo 1

M = [
 1 0 0 1 0;
 1 1 0 0 1;
 0 1 1 0 1;
 0 1 0 0 1;
 0 0 1 0 0;
 0 0 1 1 0;
 1 1 1 1 1
];

% Find all the unique combinations of rows...
S = (dec2bin(1:2^size(M,1)-1) == '1');

% Find the matching combinations...
matches = cell(0,1);

for i = 1:size(S,1)
    S_curr = S(i,:);
    
    rows = M(S_curr,:);
    rows_sum = sum(rows,1);
    
    if (all(rows_sum == 1))
        matches = [matches; {find(S_curr)}];
    end
end
Run Code Online (Sandbox Code Playgroud)

要以良好的风格化方式显示您的比赛:

for i = 1:numel(matches)
    match = matches{i};
    
    if (numel(match) == 1)
        disp(['Match found for row: ' mat2str(match) '.']);
    else
        disp(['Match found for rows: ' mat2str(match) '.']);
    end
end
Run Code Online (Sandbox Code Playgroud)

这将产生:

找到行的匹配项:7。

找到行的匹配项:[2 6]。

找到行的匹配项:[1 4 5]。

找到行的匹配项:[1 3]。

就效率而言,在我的机器中,该算法大约完成了匹配项的检测2 milliseconds