I am aware that there are discussions here about multiplying rows or columns in matrices, but I cannot figure out what to do in the following two cases. I have a matrix, lets say
m = {{1, 2, 3, 4}, {0, 1, 2, 1}}
I want to multiply the last column by 2. What I tried was replace all. Particularly:
m2=m/.{m[[All, 4]] -> 2*m[[All, 4]]}
But unsuccessfully. If I write: m[[All, 4]] -> 2*m[[All, 4]]
I get
{4, 1} -> {8, 2}
but if I use replaceAll (/.) it doesn't work. If I try to replace a row nevertheless it works fine, for example:
m3 = m /. {m[[2]] -> 2*m[[2]]}
Out: {{1, 2, 3, 4}, {0, 2, 4, 2}}
What I need to do, is actually to replace the last column's elements with the ratio of the first element of each row over the sum of elements of 2nd column. I calculate the sum as
Total[m[[All, 2]]]
and I need somehow to create a new matrix that looks like this:
{{1,2,3,1/3},{0,1,2,0}}
But I don't know how to make the calculation, and repeat it for each row (since my original matrix, not provided in this naive approach has dozens of rows). Thank you in advance for your help.
Replace[m, {b___, x_} -> {b, 2 x}, {1}]
? $\endgroup$m[[All, 4]] = 2 * m[[All, 4]]
$\endgroup$