Re: problem with Part function
- To: mathgroup at smc.vnet.net
- Subject: [mg18193] Re: problem with Part function
- From: "David Keith" <dkeith at hevanet.com>
- Date: Mon, 21 Jun 1999 22:50:35 -0400
- Organization: Hevanet Communications
- References: <7kho9q$qop@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Dario, The problem is that Part, seen here as mat[[x,y]], wants to receive its arguments as a sequence, not imbeddded in a list. mat[[{x,y}]] really means { mat[[x]], mat[[y]] }, which is a list of elements. You can use mat[[x,y]] = newValue or, if you really want to keep the points as two-element lists, say xy, use either mat[[ xy[[1]],xy[[2]] ]] which is horribly clumsy, or mat[[ Sequence@@xy ]] where xy is some {x,y}. The latter method uses Apply (given here as @@ ) to replace the head of the List xy with Sequence which splices it into Part. Here is an example: In[1]:= m = Table[0, {3}, {3}] Out[1]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}} In[2]:= m Out[2]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}} In[3]:= m[[2, 2]] = 2 Out[3]= 2 In[4]:= m Out[4]= {{0, 0, 0}, {0, 2, 0}, {0, 0, 0}} In[5]:= xy = {3, 3} Out[5]= {3, 3} In[6]:= m[[Sequence @@ {3, 3}]] = 3 Out[6]= 3 In[7]:= m Out[7]= {{0, 0, 0}, {0, 2, 0}, {0, 0, 3}} -- Dave Casubolo Dario wrote in message <7kho9q$qop at smc.vnet.net>... > >Just a little problem: >I've a matrix of zeros, say: >mat=Table[Table[0,{5}],{5}]; >then, I've a point in xy axys, >xy={3,3}; >now, I want the position {3,3} of matrix becomes 1; >if I write >mat[[xy]]=1; or mat[[{3,3}]]=1; it doesn't work; only mat[[3,3]]=1 >works, but I don't want rewrite all. >I think I've to use something like Map, Thread, Apply or so, but I don't >know what and how exactely; >please, someone help me. >THANK YOU > >Dario Casubolo >email: bubbola at workline.it > > >