I'm trying to rearrange a matrix such that all the diagonal elements are non zero.I'm not sure how to proceed. Any suggestion are highly appreciated.
A={
{a, 0, 0, 0, 0},
{0, 0, 0, d1, 0},
{0, 0, 0, 0, e1},
{0, b, 0, d2, 0},
{0, 0, c, 0, e2}
};
Diagonal[A]
Results in
{a, 0, 0, d2, e2}
I used a solution given by "corey979" for the question "Writing a program to swap rows and columns [duplicate]". To swap any two rows i and j of a general m×n matrix:
swapRij[mat_, {i_, j_}] := Block[{mat1 = mat},
mat1[[{i, j}]] = mat[[{j, i}]];
mat1 ]
If i'm using this function then I need to manually check the position and then send those row numbers as an input to that function.
It would be very difficult to arrange a big matrix.I have a matrix of size 30 x 30. I wanted to automate it.
Output i'm expecting is
A={
{a, 0, 0, 0, 0},
{0, b, 0, d2, 0},
{0, 0, c, 0, e2},
{0, 0, 0, d1, 0},
{0, 0, 0, 0, e1}
}
Where the diagonal elements are
{a, b, c, d1, e1}
I'm adding my actual matrix as well.
{{m1m1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, m1m1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, m1m1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, m4m4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m4m20, m4m21, 1, 0, 0, 0, m4m26, m4m27, 1, 0, 0},
{0, 0, 0, 0, m5m5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m5m19, 0, m5m21, 0, 1, 0, m5m25, 0, m5m27, 0, 1, 0}, {0, 0, 0, 0, 0, m6m6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m6m19, m6m20, 0, 0, 0, 1, m6m25, m6m26, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, m7m7, m7m8, m7m9, m7m10, m7m11, m7m12, 0, 0, 0, 0, 0, 0, m7m19, m7m20, m7m21, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, m8m7, m8m8, m8m9, m8m10, m8m11, m8m12, 0, 0, 0, 0, 0, 0, m8m19, m8m20, m8m21, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, m9m7, m9m8, m9m9, m9m10, m9m11, m9m12, 0, 0, 0, 0, 0, 0, m9m19, m9m20, m9m21, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m10m13, m10m14, m10m15, m10m16, m10m17, m10m18, 0, 0, 0, 0, 0, 0, m10m25, m10m26, m10m27,
0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m11m13, m11m14, m11m15, m11m16, m11m17, m11m18, 0, 0, 0, 0, 0, 0, m11m25, m11m26, m11m27, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m12m13, m12m14, m12m15, m12m16, m12m17, m12m18, 0, 0, 0, 0, 0, 0,
m12m25, m12m26, m12m27, 0, 0, 0}, {0, 0, 0, 0, 0, 0, m13m7, m13m8, m13m9, m13m10, m13m11, m13m12, 0, 0, 0, 0, 0, 0, m13m19, m13m20, m13m21, m7m19, m7m20, m7m21, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, m14m7, m14m8, m14m9, m14m10, m14m11, m14m12, 0, 0, 0, 0, 0, 0, m14m19, m14m20, m14m21, m8m19, m8m20, m8m21, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, m15m7, m15m8, m15m9, m15m10, m15m11, m15m12, 0, 0, 0, 0, 0, 0, m15m19, m15m20, m15m21,
m9m19, m9m20, m9m21, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m16m13, m16m14, m16m15, m16m16, m16m17, m16m18, 0, 0, 0, 0, 0, 0, m16m25, m16m26, m16m27, m10m25, m10m26, m10m27},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m17m13, m17m14, m17m15, m17m16, m17m17, m17m18, 0, 0, 0, 0, 0, 0, m17m25, m17m26, m17m27, m11m25, m11m26, m11m27}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, m18m13, m18m14, m18m15, m18m16, m18m17, m18m18, 0, 0, 0, 0, 0,
0, m18m25, m18m26, m18m27, m12m25, m12m26, m12m27}, {0, 0, 0, m7m19, m7m20, m7m21, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, m8m19, m8m20, m8m21, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, m9m19, m9m20, m9m21, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, m10m25, m10m26, m10m27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, m11m25, m11m26, m11m27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, m12m25, m12m26, m12m27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, m5m19, m6m19, m7m19, m8m19, m9m19, m13m19, m14m19, m15m19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, m4m20, 0, m6m20, m7m20, m8m20, m9m20, m13m20, m14m20, m26m12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, m4m21, m5m21, 0, m7m21, m8m21, m9m21, m13m21, m14m21, m15m21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, m5m25, m6m25, 0, 0, 0, 0, 0, 0, m10m25, m11m25, m12m25, m16m25, m17m25, m18m25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, m4m26, 0, m6m26, 0, 0, 0, 0, 0, 0, m10m26, m11m26, m12m26, m16m26, m17m26, m29m18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0}, {0, 0, 1, m4m27, m5m27, 0, 0, 0, 0, 0, 0, 0, m10m27, m11m27, m12m27, m16m27, m17m27, m18m27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
Thank you,