Re: Replacing a part of a list/matrix?

*To*: mathgroup at smc.vnet.net*Subject*: [mg2221] Re: Replacing a part of a list/matrix?*From*: withoff (David Withoff)*Date*: Tue, 17 Oct 1995 02:33:42 -0400*Organization*: Wolfram Research, Inc.

In article <45fl35$2t8 at ralph.vnet.net> ianc at wri.com (Ian Collier) writes: >In article <45d22j$63m at ralph.vnet.net>, olee at ripco.com (O. Lee) wrote: > >> I am trying to assign a value to a matrix position, such as: >> >> pop[[ x,y ]] = value >> >> in a module function. When I try manually assigning a value >> to the matrix position outside of the module, it works. When >> I run the function with the statement, Mathematica gives me >> the following error message: >> >> Part::setps: >> >> {{1, 1, 0, 0}, <<1>>} >> in assignment of part is not a symbol. >> >> Can anyone tell me how to get the assignment statement to >> work! I isolated the problem in the function to this line >> and I am sure that what I just described was the problem. >> I have tried using the command: >> >> pop=ReplacePart[pop,value,{x,y}] >> >> but it gives me even more error messages! Help me please! >> I appreciate any help. Thanks. >> >> O. Lee > >ReaplacePart is the way to do this. It workes in this example: > >In[9]:= > mat = {{a,b},{c,d}} >Out[9]= > {{a, b}, {c, d}} >In[11]:= > mat = ReplacePart[ mat, 4, {1,2}] >Out[11]= > {{a, 4}, {c, d}} >In[12]:= > mat >Out[12]= > {{a, 4}, {c, d}} Part assignments (assignments of the form mat[[1,2]] = value) will also work fine. For example: In[1]:= pop = Array[e, {3, 3}] Out[1]= {{e[1, 1], e[1, 2], e[1, 3]}, {e[2, 1], e[2, 2], e[2, 3]}, > {e[3, 1], e[3, 2], e[3, 3]}} In[2]:= pop[[2,2]] = value Out[2]= value In[3]:= pop Out[3]= {{e[1, 1], e[1, 2], e[1, 3]}, {e[2, 1], value, e[2, 3]}, > {e[3, 1], e[3, 2], e[3, 3]}} The fact that you got the Part::setps message indicates that you must have used a part assignment to assign parts to the value of something that wasn't a symbol. Part assignments only work with symbols. In[4]:= mat[3] = Array[e, {3, 3}] Out[4]= {{e[1, 1], e[1, 2], e[1, 3]}, {e[2, 1], e[2, 2], e[2, 3]}, > {e[3, 1], e[3, 2], e[3, 3]}} In[5]:= mat[3][[2,2]] = value Part::setps: mat[3] in assignment of part is not a symbol. Out[5]= value Also, the content of the message Part::setps: {{1, 1, 0, 0}, <<1>>} in assignment of part is not a symbol. indicates that the left-hand side of the assignment was not only not a part of a symbol, it was part of a fully evaluated matrix. There are several ways that this could happen, as in In[9]:= {{1, 1, 0, 0}, {0, 0, 1, 1}}[[1, 1]] = value Part::setps: {{1, 1, 0, 0}, {0, 0, 1, 1}} in assignment of part is not a symbol. Out[9]= value but in order to know exactly what to do about it we'd have to see the rest of your example. There are easy ways to do all of this, and the stuff that you described should work fine, so something else significant must be going on. Dave Withoff Research and Development Wolfram Research