Cartesian product of row-indices in Matlab -


i have binary matrix a of dimension mxn m>n in matlab. want construct matrix b of dimension cxn listing row wise each element of cartesian product of row indices of ones contained in a. more clear consider following example.

example:

  %m=4;   %n=3;    a=[1 0 1;      0 0 1;      1 1 0;      0 0 1];    %column 1: "1" @ rows {1,3}   %column 2: "1" @ row {3}   %column 3: "1" @ rows {1,2,4}    %hence, cartesian product {1,3}x{3}x{1,2,4}    %{(1,3,1),(1,3,2),(1,3,4),(3,3,1),(3,3,2),(3,3,4)}     %i construct b disposing row-wise each 3-tuple in cartesian product    %c=6   b=[1 3 1;     1 3 2;     1 3 4;     3 3 1;     3 3 2;     3 3 4]; 

you can cartesian product combvec command, example:

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 

you can expand unknown number of columns using associative property of product.

[x y]=find(a);  u_y=unique(y);  b=x(y==u_y(1)).';  i=2:length(u_y)     b=combvec(b, x(y==u_y(i)).'); end  b=b.'; 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -