Re: Modifying an entry in array specified by a list of arbitrary length

• To: mathgroup at smc.vnet.net
• Subject: [mg127170] Re: Modifying an entry in array specified by a list of arbitrary length
• From: "Nasser M. Abbasi" <nma at 12000.org>
• Date: Thu, 5 Jul 2012 06:10:52 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com

```On 7/4/2012 2:31 AM, samid.hoda at gmail.com wrote:
> Hi,
>
> I want to edit an element whose position is specified by an arbitrary list that is a result of a computation (the list is not of fixed length). Something like
>
> i = {1,2,3}
> Extract[X,i]+=1
>
> except that Extract passes X by value instead of by reference. Anyone have any ideas?
>
> Thanks,
> Sam
>

Extract, exracts the elements out of the list. i.e. you get
a copy of those elements.

Then when you modify those elements, you are not modifying
the elements in the original list any more. You modifying
the copy of those elements you extracted.

For your example above, you do not need to extract, you
can just do the simple indexing and work on the element
while they are sitting in the list

-----------------------------
lis = Range[10];
idx = {1,2,3};
lis[[idx]]

Out[48]= {1,2,3}

lis[[idx]] = lis[[idx]]+1;
lis[[idx]]
Out[50]= {2,3,4}
---------------------------------

If you insist on using Extract, then you can extract
the elements, modify them, then have to put them back into
the list to update the list

-------------------
lis = Range[10];
idx = {1,2,3};
tmp = Extract[lis,List/@idx];
tmp = tmp+1;       (*update process*)
lis[[idx]] = tmp;  (*put them back*)
lis[[idx]]
-----------------
Out[56]= {2,3,4}

--Nasser

```

• Prev by Date: Re: Mathematica 8.0.4 with Lion 7.0.2
• Next by Date: Re: Higher precision in Error function Erf[] needed.
• Previous by thread: Re: Modifying an entry in array specified by a list of arbitrary length
• Next by thread: NonlinearModelFit to multiple data sets simultaneously?