1
votes

I have a square matrix B and I want to extract its submatrix which has consecutive row numbers and column numbered 1 through k, with k every natural number no more than n (size of my matrix). It also needs to have non-zero main diagonal entries.

Furthermore, I want to store the submatrices in an array form, (the next step is to check if their determinants are positive, but I won't include that in this question. Here is the code I have built:

for i = 1:n
    for j = 1:n-i+1
        submat2{i,j} = B([j:j+i-1],[1:i]);
        for k = 1:i
            maindiag{i,j,k} = prod((submat2{i,j}(i,i) ~= 0));
        end
        matmaindiag = []
        for l = 1:size(maindiag(i,j,:),3)
            matmaindiag = [matmaindiag cell2mat(maindiag(i,j,l))]

            if prod(matmaindiag ~= 0)
                boundsub{end+1} =  submat2{i,j};
            end
        end
    end
end

Is there any better way to do this?

For example, if I have:

B =

 6     7     8     9   
11    12    13    14   
 0    17    18    19   
 0     0    23    24 

then the submatrices I would like to extract are:

B([1],[1]), B([1],[2]), B([1,2],[1,2]), B([2,3],[1,2]), B([1,2,3],[1,2,3]), B([2,3,4],[1,2,3]), and B itself

since they:

  1. consisted of entries from columns of B that numbered consecutively from 1 through k (the size of the submatrix itself) and

    1. From the consecutively numbered rows of B

    2. Don't have any zero-valued diagonal entries

Thanks for any help and thoughts :)

1
Can you please add an example of B and what would you like the result to be in that case?Vahe Tshitoyan
i did, thanks for the feedbackbotengboteng
You say that row and column numbers must both be 1:k, but you also want to extract B([2,3],[1,2]). Should only one of the rows/columns be 1:k, or was this entry an error?Poelie
the columns are consecutively numbered starting from 1, the rows are also consecutively numbered, but not necessarily starts from 1botengboteng

1 Answers

1
votes

I have come up with this

n = size(B,1)
for i = 1:n
    for j = 1:n-i+1
    submat{i,j} = B([j:j+i-1],[1:i]);

     end
end
bousub = []
for i = 1:n
    for j = 1:n-i+1
     dia = diag(submat{i,j});
        if (prod(dia) ~= 0)    
        bousub{end+1} =  submat{i,j};

        end
    end
end