1

I have an arbitrary n-by-n matrix. I want to look at sets of columns and rows of the matrix and do some analysis on them, for example by setting all elements of a specific set of rows and columns equal to zero. To do this I need to analyse all combinations of rows and columns.

For example, if n=3 the process selects the row and columns 1, 2, 3, 12, 13, 23, 123 in succession and creates a new variable for each row and column.

I am currently the technique below for a matrix of size 4:

H = [some 4-by-4 matrix] 

for i1 = 1:n
    for i2 = 1:n
        for i3 = 1:n
            for i4 = 1:n

                    % Set all rows and columns of all variables equal to 0

                    H(:,i1) = 0;
                    H(i1,:) = 0;

                    H(:,i2) = 0;
                    H(i2,:) = 0;

                    H(:,i3) = 0;
                    H(i3,:) = 0;

                    H(:,i4) = 0;
                    H(i4,:) = 0;

                    % Some more analysis on i1, i2, i3, i4...

            end
        end
    end
end 

This is an extremely crude method but it seems to work. Obviously, this technique looks at the set (1,1,1,1) which is equivalent to just (1) first, then (1,1,1,2) which is equivalent to (1,2), then (1,1,1,3) which is equivalent to (1,3)... and so on...

The problem here is that this is not a general process for any matrix of size n, this is only a crude process for a matrix of size 4.

Is there any way to generalise the process so that it works for any arbitrary n-by-n matrix?

Thanks!

Owen
  • 13
  • 6
  • This seems as a special case application of a [general question](http://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors) asked and answered by Louis Mendo. You should try to apply his solution to your problem and if you are stuck ask again. – The Minion Sep 11 '14 at 10:21
  • Thanks for this @TheMinion I will have a look at this now :) . – Owen Sep 11 '14 at 10:59
  • So the row and column indices would be the same in all those combinations? – Divakar Sep 11 '14 at 13:46
  • Yes @Divakar, that is correct. For example, if I select the set (1,1,1,3), or (1,3), then I wish to choose the row and column that the element H(1,1) and H(3,3) are on. – Owen Sep 11 '14 at 14:41

2 Answers2

2

You can reduce the arbitrary number of loops to one:

for k = 1:2^n-1
    ind = dec2bin(k,n)=='1';
    H(ind,:) = 0;
    H(:,ind) = 0;
end

The trick is to use just one loop to create a logical index (ind) that tells which columns will be selected. So for n=4 the variable ind takes the values [0 0 0 1], [0 0 1 0], [0 0 1 1], ... [1 1 1 1].

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
0

Here is a neat way to do that with only two for loops and no magic function. It uses the binary representation of the integer numbers to decide whether to zero out a column and a row.

I just fix some values for the test

n = 3;
Mat = rand(n,n);

Then, we know that there are 2^n combinations, so let's number them from 0 to 2^n-1:

for tag=0:2^n-1

We make a copy to keep the original matrix untouched

    myMat = Mat;

Now loop on the row and columns

    for (i=1:n)

Here is the trick: if the i-th bit of tag (in binary) is 1, then we zero out the column and row, otherwise we keep it untouched.

        if ( mod( floor(tag/2^(i-1)), 2) == 1 )
            myMat(:,i) = 0;
            myMat(i,:) = 0;
        end
    end

Finally display to check that we have what we need.

    myMat
end
Dr_Sam
  • 1,818
  • 18
  • 24