MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: setps problem/how to set matrix elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65835] Re: setps problem/how to set matrix elements
  • From: "J Siehler" <jsiehler at gmail.com>
  • Date: Tue, 18 Apr 2006 06:56:32 -0400 (EDT)
  • References: <e1vet0$9db$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Giving your function the HoldFirst attribute will cause it to treat the
r on the righthand side as exactly what you've passed it, instead of
replacing it with the value of what you've passed it (which is what's
generating your error).

  In[10]:=
FSD[r_,xdim_,ydim_]:=Module[{},r[[1,1]]=5;]
SetAttributes[FSD,HoldFirst]

In[12]:=
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}};

In[13]:=
FSD[r,256,256]

In[14]:=
r

Out[14]=
{{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}}

-------
If it were me, I would write the function so that it behaves a little
differently, and returns a modified list rather than modifying the list
you've passed.  Use assignment if you actually want to change your r.

In[42]:=
Clear[FSD]
ClearAttributes[FSD,HoldFirst]

In[45]:=
FSD[r_,xdim_,ydmi_]:=ReplacePart[r,5,{1,1}]

In[46]:=
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}};

In[48]:=
FSD[r,256,256]

Out[48]=
{{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[49]:=
r

Out[49]=
{{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[50]:=
r=FSD[r,256,256]

Out[50]=
{{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[51]:=
r

Out[51]=
{{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}}


  • Prev by Date: parenthesis in reactions...
  • Next by Date: Re: unable to FullSimplify
  • Previous by thread: Re: setps problem/how to set matrix elements
  • Next by thread: Re: setps problem/how to set matrix elements