Re: setps problem/how to set matrix elements
- To: mathgroup at smc.vnet.net
- Subject: [mg65843] Re: [mg65778] setps problem/how to set matrix elements
- From: "Szabolcs Horvát" <szhorvat at gmail.com>
- Date: Tue, 18 Apr 2006 06:56:39 -0400 (EDT)
- References: <200604170628.CAA08745@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 4/17/06, Robert James Williamson <sparkles at stanford.edu> wrote: > > I get General::setps as an error for the following: > > FSD[r_, xdim_, ydim_] := Module[{}, r[[1, 1]] = 5;] > > FSD[r, 256, 256] <-- This command generates the error. > > Where r is defined as: > > r = {{0.5, 0.5, 0.5, 0.5}, {0.5, 0.5, 0.5, 0.5}, {0.5, > 0.5, 0.5, 0.5}, {0.5, 0.5, 0.5, 0.5}}; > > I'm confused because I have no problem when I type in: > > r[[1,1]] = 5 > > This correctly sets the 1,1 entry of the matrix to 5. > > Any input on this? Or point me to the FAQ? > > Kind regards, and many thanks. > > Rob > > I'll name the matrix as p here to distinguish it from the argument of the FSD function. In[1]:= FSD[r_,xdim_,ydim_]:=Module[{a=0},r[[1,1]]=5;] In[2]:= p={{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5}}; In[3]:= FSD[p,256,256] From In[3]:= Set::setps: {{0.5, 0.5, 0.5, 0.5}, {<<4>>}, {<<4>>}, {0.5, 0.5, 0.5, 0.5}} in assignment of part is not a symbol. And here you get an error, because when FSD[p,256,256] is evaluated, first p is replaced by the matrix {{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5}}, then r (in the definition of FSD) is replaced by this matrix value, and you get {{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5}}[[1,1]] = 5; which is illegal. This is explained in the documentation of the error message (in version 5.2, not all messages are documented in all versions). You must prevent the first argument of FSD from being evaluated, so r will be replaced by p instead of the value of p. See the documentation of attribute HoldFirst. In[4]:= SetAttributes[FSD, HoldFirst] In[5]:= FSD[p,256,256] In[6]:= p Out[6]= {{5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5},{0.5,0.5,0.5,0.5}} After applying the attribute, the function works correctly, and p[[1,1]] is set to 5. Szabolcs Horvát
- References:
- setps problem/how to set matrix elements
- From: Robert James Williamson <sparkles@stanford.edu>
- setps problem/how to set matrix elements