0
$\begingroup$

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?

$\endgroup$
2
$\begingroup$

Operation on m itself:

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

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

m // MatrixForm

enter image description here

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}
  }
 ]
$\endgroup$
  • $\begingroup$ Where you reading my mind? :) I think you posted 5 seconds before me, so I will delete my post. good answer. $\endgroup$ – Nasser Mar 9 '15 at 8:34
  • $\begingroup$ @Nasser oh, it seems so :) Sorry and thanks :) $\endgroup$ – Kuba Mar 9 '15 at 8:35
  • $\begingroup$ 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. $\endgroup$ – Keith Lim Mar 9 '15 at 8:40
  • $\begingroup$ After divide, the sum of each column should give value of 1. $\endgroup$ – Keith Lim Mar 9 '15 at 8:41
  • $\begingroup$ @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? $\endgroup$ – Kuba Mar 9 '15 at 8:43
1
$\begingroup$
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.}}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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