MathGroup Archive 2012

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

Search the Archive

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

On 7/4/2012 2:31 AM, samid.hoda at 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};

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

lis[[idx]] = lis[[idx]]+1;
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*)
Out[56]= {2,3,4}


  • 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?