# Reducing the zero row/column of a matrix

I have a $$8\times 8$$ matrix, whose 2,3,5,8-th row and columns are zero:

mat = {{1/2, 0, 0, 0, 0, 1/2 E^(-I t w1), -(1/2) E^(-2 I t w1 - I t w2),
0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0,
0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0,
0}, {-(1/2) E^(I theta1 - I t w2), 0, 0, 0, 0,
1/2 E^(I theta1 - I t w1 - I t w2),
1/2 E^(I theta1 - 2 I t w1 - 2 I t w2),
0}, {1/2 E^(I theta1 + I theta2 - I t w2), 0, 0, 0,
0, -(1/2) E^(I theta1 + I theta2 - I t w1 - I t w2),
1/2 E^(I theta1 + I theta2 - 2 I t w1 - 2 I t w2), 0}, {0, 0, 0, 0,
0, 0, 0, 0}}


The above matrix looks like this: What I want to do is, by removing the row/column with zero entries, I want to condense this into $$4\times 4$$ matrix. How can I do this?

• fyi, MatrixForm of your code do not match what you show as screen shot Jan 10, 2022 at 4:14

mat0 = {{m[1, 1], 0, 0, m[1, 4], 0, m[1, 6], m[1, 7], 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{m[4, 1], 0, 0, m[4, 4], 0, m[4, 6], m[4, 7], 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{m[6, 1], 0, 0,  m[6, 4], 0, m[6, 6], m[6, 7], 0},
{m[7, 1], 0, 0, m[7, 4], 0, m[7, 6], m[7, 7], 0},
{0, 0, 0, 0, 0, 0, 0, 0}};

MatrixForm @ mat0 1. You can use Part as follows:
mat1 = mat0[[{1, 4, 6, 7}, {1, 4, 6, 7}]];

MatrixForm @ mat1 1. You can also use DeleteCases + Transpose + Nest
mat2 = Nest[Transpose @* DeleteCases[{0 ..}], mat0, 2];

MatrixForm @ mat2 The display of the matrix you have do not agree with the code. But you can do

mat = {{m[1, 1], 0, 0, m[1, 4], 0, m[1, 6], m[1, 7], 0}, {0, 0, 0, 0,
0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {m[4, 1], 0, 0, m[4, 4], 0,
m[4, 6], m[4, 7], 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {m[6, 1], 0, 0,
m[6, 4], 0, m[6, 6], m[6, 7], 0}, {m[7, 1], 0, 0, m[7, 4], 0,
m[7, 6], m[7, 7], 0}, {0, 0, 0, 0, 0, 0, 0, 0}}; DeleteCases[mat, {0 ..}, Infinity];
Transpose@DeleteCases[Transpose[%], {0 ..}, Infinity] Define:

delEmpty = {0 ..} -> Nothing;


Execute:

(Transpose[Transpose[(mat /. delEmpty)] /. delEmpty]) // MatrixForm


$$\left( \begin{array}{cccc} m(1,1) & m(1,4) & m(1,6) & m(1,7) \\ m(4,1) & m(4,4) & m(4,6) & m(4,7) \\ m(6,1) & m(6,4) & m(6,6) & m(6,7) \\ m(7,1) & m(7,4) & m(7,6) & m(7,7) \\ \end{array} \right)$$