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:
consisted of entries from columns of
B
that numbered consecutively from 1 through k (the size of the submatrix itself) andFrom the consecutively numbered rows of B
Don't have any zero-valued diagonal entries
Thanks for any help and thoughts :)
B
and what would you like the result to be in that case? – Vahe Tshitoyan