Question about assignments and variables
(* For example *) SP = SparseArray[{},5] or SP = Range[5]
now we want to work with this array in some another function :
(* example *) Fun[array_]:= array[[3]] = 100 ; (* set cell №3 equal to 100*)
then we eval
Fun[SP]
ERROR! output will be an Error like: Set::write Tag SparseArray in ... is Protected.
So what is the right way to change the arguments of function in function (non-pure-functions)? How to creare analog-like of Part[]
?
maybe smth like:
Clear[f]; f[a_]:=Set[Symbol[a][[3]],100]; A =SparseArray[{},5]; f["A"];
But it's error again
I believe that Chris Degnen's method should generally be avoided.
Mathematica provides a better way: the Hold attributes.
a = Range[5];
SetAttributes[fun, HoldFirst]
fun[array_] := array[[3]] = 100
fun[a];
a
{1, 2, 100, 4, 5}
As a "pure function":
b = Range[5];
fun2 = Function[array, array[[3]] = 100, HoldFirst];
fun2[b];
b
{1, 2, 100, 4, 5}