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:


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:


everything works as expected.

What is the problem and how can I write a function to modify a matrix "in place"?

  • 1
    $\begingroup$ You need to set HoldFirst attribute for your function. Also, do not use Module if you are not going to scope anything, it will only slow you down. $\endgroup$ – Kuba Apr 28 '14 at 18:10
  • $\begingroup$ @Kuba, thank you for the comment and for the links. $\endgroup$ – user1541776 Apr 28 '14 at 23:55

To prevent m from evaluating to its value, use the following command:

SetAttributes[MultiplyRowByFactor, HoldFirst];

Just once, right after the declaration of MultiplyRowByFactor. Now the first argument of this function, which is just m, is held, and everything works.


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