如何比较二进制矩阵的列并比较matlab中的元素?

How to compare columns of a binary matrix and compare elements in matlab?

我有 [句子*单词] 矩阵,如下所示

out = 0 1 1 0 1
      1 1 0 0 1
      1 0 1 1 0
      0 0 0 1 0

我想以一种方式处理这个矩阵,应该告诉 "sentence number 2" 中的 W1 & W2 和 "sentence number 4" 以相同的值出现,即 1 10 0。输出应如下所示:

output{1,2}= 2 4  

output{1,2} 告诉单词编号 1 和 2 出现在句子编号 2 和 4 中具有相同的值。

在比较 W1 & W2 之后,下一个候选者应该是 W1 & W3,它在 sentence 3 & sentence 4 中出现相同的值

output{1,3}= 3 4

依此类推,直到每个 nth 单词都与其他所有单词进行比较并保存。

您可以使用 :

轻松获得大小为#words-by-#words-by-#sentences 的逻辑矩阵
coc = bsxfun( @eq, permute( out, [3 2 1]), permute( out, [2 3 1] ) );

这个逻辑数组 occ( wi, wj, si ) 为真当且仅当单词 wi 和单词 wj 出现在句子 si 中具有相同的值。

要从 coc 获取 output 元胞数组,您需要

nw = size( out, 2 ); %// number of words
output = cell(nw,nw);
for wi = 1:(nw-1)
    for wj = (wi+1):nw
        output{wi,wj} = find( coc(wi,wj,:) );
        output{wj,wi} = output{wi,wj}; %// you can force it to be symmetric if you want
    end
end

这将是一种 vectorized 方法 -

%// Get number of columns in input array for later usage 
N = size(out,2);

%//  Get indices for pairwise combinations between columns of input array
[idx2,idx1] = find(bsxfun(@gt,[1:N]',[1:N])); %//'

%// Get indices for matches between out1 and out2. The row indices would
%// represent the occurance values for the final output and columns for the 
%// indices of the final output.
[R,C] = find(out(:,idx1) == out(:,idx2))

%// Form cells off each unique C (these will be final output values)
output_vals = accumarray(C(:),R(:),[],@(x) {x})

%// Setup output cell array
output = cell(N,N)

%// Indices for places in output cell array where occurance values are to be put
all_idx = sub2ind(size(output),idx1,idx2)

%// Finally store the output values at appropriate indices
output(all_idx(1:max(C))) = output_vals