Cartesian product of row-indices in Matlab
<p>I have a binary matrix <code>A</code> of dimension <code>mxn</code> with <code>m&gt;n</code> in Matlab. I want to construct a matrix <code>B</code> of dimension <code>cxn</code> listing row wise each element of the Cartesian product of the row indices of the ones contained in <code>A</code>. To be more clear consider the following example.</p>

<p>Example: </p>

<pre><code>  %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];
</code></pre>