3
$\begingroup$

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:

enter image description here

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?

$\endgroup$
1
  • $\begingroup$ fyi, MatrixForm of your code do not match what you show as screen shot $\endgroup$
    – Nasser
    Jan 10, 2022 at 4:14

3 Answers 3

5
$\begingroup$
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

enter image description here

  1. You can use Part as follows:

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

MatrixForm @ mat1

enter image description here

  1. You can also use DeleteCases + Transpose + Nest

mat2 = Nest[Transpose @* DeleteCases[{0 ..}], mat0, 2];

MatrixForm @ mat2

enter image description here

$\endgroup$
4
$\begingroup$

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}};

Mathematica graphics

DeleteCases[mat, {0 ..}, Infinity];
Transpose@DeleteCases[Transpose[%], {0 ..}, Infinity]

Mathematica graphics

$\endgroup$
4
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.