MATLAB: How to remove rows and columns from two matrices

eliminationmatrices

I have to remove columns and rows from two matrices A and B (consist of 1 and 0), where all row values are zeros. The process is repeated until we get a matrices A and B that consist only of zero values. Also, I need to know the order of rows elimination.Thanks!

Best Answer

A = [0 0 0 0 0; 1 0 0 0 0; 0 0 0 0 0; 0 0 1 0 0; 0 0 0 0 0];
B = [0 0 0 0 0; 1 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0; 0 1 0 0 1];
rows = 1:size(A, 1);
roworder = {};
while ~all(A(:) == A(1)) || ~all(B(:) == B(1))
tokeep = any(A, 2) | any(B, 2);
roworder = [roworder, rows(~tokeep)];
A = A(tokeep, tokeep);
B = B(tokeep, tokeep);
rows = rows(tokeep);
end
roworder = [roworder, rows];
celldisp(roworder)