# Matrix operation (simplify)

I had a $6\times 6$ matrix

{{1., 0., 0., 0., 0, 0},
{0, 0.538752, 0.382352, 0.0788956, 2.35142*10^-7, 0},
{0, 0.121876, 0.449018, 0.405176, 0.0239311, 0},
{0, 0.0147839, 0.264751, 0.596752, 0.123713, 0},
{0, 0, 0.0294091, 0.381909, 0.563762, 0.0249205},
{0, 0, 0., 0., 0., 1.}}


and I wanted to multiply row 3 by 0.8, row 4 by 1.2 and divide each column by the sum of each column except the first and last column.

Instead of doing it manually by editing it on the matrix, is there an alternative or Mathematica code to do so?

Operation on m itself:

m[[{3, 4}]] *= {.8, 1.2};

m[[All, 2 ;; -2]] = Transpose[Normalize[#, Total] & /@ Transpose[m[[All, 2 ;; -2]]]];

m // MatrixForm


after a talk, if you want to normalize (with Total norm) rows then skip transposing:

m[[2 ;; -2]] = Normalize[#, Total] & /@ m[[2 ;; -2]];


and if you don't want to modify m you can do:

Fold[
MapAt[#2[[1]], #, #2[[2]]] &,
m,
{{.8 # &, 3},
{1.2 # &, 4},
{Normalize[#, Total] &, 2 ;; -2}
}
]

• Where you reading my mind? :) I think you posted 5 seconds before me, so I will delete my post. good answer. Commented Mar 9, 2015 at 8:34
• @Nasser oh, it seems so :) Sorry and thanks :)
– Kuba
Commented Mar 9, 2015 at 8:35
• Hi. Sorry for the confusing question. What i really mean is that each column is divided by to sum of its column. For example, Sum of column 2 is 1.22756, so each element of column 2 is divided by 1.22756. Same goes to column 3, 4, 5. As the sum of first and last column is 1, 1/1 is still 1. Commented Mar 9, 2015 at 8:40
• After divide, the sum of each column should give value of 1. Commented Mar 9, 2015 at 8:41
• @KeithLim Sum for 2nd column is 0.675412 after multiplications of rows. But at the end it is 1 you can check by m[[All, 2]] // Total. Or have I missed something?
– Kuba
Commented Mar 9, 2015 at 8:43
list = {{1., 0., 0., 0., 0, 0}, {0, 0.538752, 0.382352, 0.0788956,
2.35142*10^-7, 0}, {0, 0.121876, 0.449018, 0.405176, 0.0239311,
0}, {0, 0.0147839, 0.264751, 0.596752, 0.123713, 0}, {0, 0,
0.0294091, 0.381909, 0.563762, 0.0249205}, {0, 0, 0., 0., 0.,
1.}};
list2 = {1, 1, 0.8, 1.2, 1, 1} list;
Transpose[MapAt[#/Total[#] &, Transpose[list2], {{2}, {3}, {4}, {5}}]]
(*{{1., 0., 0., 0., 0., 0}, {0, 0.823788, 0.351208, 0.0525604,
3.21512*10^-7, 0}, {0., 0.149085, 0.329955, 0.215943, 0.026177,
0.}, {0., 0.0271267, 0.291823, 0.477068, 0.202985, 0.}, {0, 0.,
0.0270136, 0.254428, 0.770838, 0.0249205}, {0, 0., 0., 0., 0., 1.}}*)