Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10793

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

From "Nasser M. Abbasi" <nma@12000.org>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Modifying an entry in array specified by a list of arbitrary length
Date 2012-07-05 10:11 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jt3p94$10a$1@smc.vnet.net> (permalink)

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Modifying an entry in array specified by a list of arbitrary length "Nasser M. Abbasi" <nma@12000.org> - 2012-07-05 10:11 +0000

csiph-web