I'd like to write a function, that would take a matrix as an input parameter and would change it by multiplying one of its rows by a factor.
I try to do it in the following way:
matrix=Table[i*j,{i,Range@2},{j,Range@2}]
MultiplyRowByFactor[m_,factor_,iRow_]:=Module[{},m[[All,iRow]]*=factor;m]
MultiplyRowByFactor[matrix,2,1]
and get an error message
Set::setps: "{{1,2},{2,4}} in the part assignment is not a symbol. "
Of course, if I change the matrix outside of the function:
matrix[[All,1]]*=2;
matrix
everything works as expected.
What is the problem and how can I write a function to modify a matrix "in place"?
HoldFirstattribute for your function. Also, do not useModuleif you are not going to scope anything, it will only slow you down. – Kuba Apr 28 '14 at 18:10