How can I remove rows and columns from two matrices?

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!

2 Comments

Please provide a short example showing inputs and desired outputs with explanations.
For example, I have matrix
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]
and
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]
The size of matrices is the same, and diagonal is always zero.In the third row of both matrix are zero values, so, the 1st and 3rd row and columns will be deleted. Now A=[0 0 0; 0 0 0; 0 0 0] and B=[0 0 0; 0 0 0; 1 0 1]. The process is repeated, until we get a matrices with all zero values, or 1 (as the only value in the matrix).At the and I will get A=[0] and B=[1]. The order of elimination is 1)1 and 3 row, 2)2 and 4 row, 3)5th row.

Sign in to comment.

 Accepted 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)

1 Comment

Thank you! I tried to do the same if the condition is that the values in the columns of matrices are zero. I transponse a matrices and used your code, but it doesn't work. I will appreciate a help...

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!