Matlab 中行索引的笛卡尔积

Cartesian product of row-indices in Matlab

我在 Matlab 中有一个维度 mxnm>n 的二进制矩阵 A。我想构建一个维度 cxn 的矩阵 B 逐行列出 A 中包含的行索引的笛卡尔积的每个元素。为了更清楚地考虑以下示例。

示例:

  %m=4;
  %n=3;

  A=[1 0 1;
     0 0 1;
     1 1 0;
     0 0 1];

  %column 1: "1" are at rows {1,3}
  %column 2: "1" are at row {3}
  %column 3: "1" are at rows {1,2,4}

  %Hence, the Cartesian product {1,3}x{3}x{1,2,4} is 
  %{(1,3,1),(1,3,2),(1,3,4),(3,3,1),(3,3,2),(3,3,4)} 

  %I construct B by disposing row-wise each 3-tuple in the Cartesian product

  %c=6

 B=[1 3 1;
    1 3 2;
    1 3 4;
    3 3 1;
    3 3 2;
    3 3 4];

您可以使用 combvec 命令获取笛卡尔积,例如:

A=[1 0 1;...
   0 0 1;...
   1 1 0;...
   0 0 1];

[x y]=find(A);

B=combvec(x(y==1).',x(y==2).',x(y==3).').';

% B =
%    1   3   1
%    3   3   1
%    1   3   2
%    3   3   2
%    1   3   4
%    3   3   4

您可以使用产品的关联 属性 将其扩展到未知数量的列。

[x y]=find(A);

u_y=unique(y);

B=x(y==u_y(1)).';

for i=2:length(u_y)
    B=combvec(B, x(y==u_y(i)).');
end

B=B.';

一个解决方案(没有工具箱):

A=  [1 0 1;
     0 0 1;
     1 1 0;
     0 0 1];



[ii,jj] = find(A)

kk = unique(jj); 

for i = 1:length(kk)
    v{i} = ii(jj==kk(i));
end

t=cell(1,length(kk));
[t{:}]= ndgrid(v{:});

product = []
for i = 1:length(kk)
    product = [product,t{i}(:)];
end

简而言之,我将使用 find 生成笛卡尔积所需的索引,然后使用 ndgrid 执行这些索引的笛卡尔积。这样做的代码是:

clear
close all
clc

A = [1 0 1;
     0 0 1;
     1 1 0;
     0 0 1];

[row,col] = find(A);
[~,ia,~] = unique(col);
n_cols = size(A,2);
indices = cell(n_cols,1);

for ii = 1:n_cols-1
  indices{ii} = row(ia(ii):ia(ii+1)-1);
end
indices{end} = row(ia(end):end);

cp_temp = cell(n_cols,1);
[cp_temp{:}] = ndgrid(indices{:});

cp = NaN(numel(cp_temp{1}),n_cols);
for ii = 1:n_cols
  cp(:,ii) = cp_temp{ii}(:);
end
cp = sortrows(cp);
cp

您可以使用 accumarray 来获取具有每列非零元素的行索引的向量。这适用于 任意数量的列 :

[ii, jj] = find(A);
vectors = accumarray(jj, ii, [], @(x){sort(x.')});

然后应用this answer有效地计算这些向量的笛卡尔积:

n = numel(vectors);
B = cell(1,n);
[B{end:-1:1}] = ndgrid(vectors{end:-1:1});
B = cat(n+1, B{:});
B = reshape(B,[],n);

在您的示例中,这给出了

B =
     1     3     1
     1     3     2
     1     3     4
     3     3     1
     3     3     2
     3     3     4