Re: Assigning part of indexed object
- To: mathgroup at smc.vnet.net
- Subject: [mg119803] Re: Assigning part of indexed object
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 23 Jun 2011 07:25:00 -0400 (EDT)
On 6/22/11 at 7:29 AM, fabrice.laussy at n0spam-gmail.com (Fabrice P.
Laussy) wrote:
>How do you set the part of an indexed object, say:
>m[1] = Range[5];
>is my indexed object (I could have m[2], etc.). Now I want to do
>something like:
>m[1][[1]] = 10;
>with effect of having m[1] read:
>{10, 2, 3, 4, 5}
>but of course the line above evaluates to 1 = 10.
>This could be achieved by having a non-indexed object get in the
>way, e.g., m1=m[1], assign m1 and then return to m[1]=m1, but I
>believe there should a more elegant way.
The key is understanding where the list {1,2,3,4,5} is stored.
You can change m such that m[1][[1]] evaluates to 10 as follows:
In[1]:= m[1] = Range[5];
In[2]:= m[1]
Out[2]= {1,2,3,4,5}
In[3]:= DownValues[m] = ReplacePart[DownValues[m], {1, 2, 1} -> 10];
In[4]:= m[1]
Out[4]= {10,2,3,4,5}