Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14764
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Adding value to some elements of a list |
| Date | 2013-06-06 11:23 +0000 |
| Message-ID | <koprfk$cgn$1@smc.vnet.net> (permalink) |
| References | <komoi3$1un$1@smc.vnet.net> |
| Organization | Time-Warner Telecom |
On 05/06/2013 08:15, Joerg Lenhardt wrote:
> Hello,
>
> I have two lists. One containing values, another containing indexes to
> the first list. I want to add a value to all elements of the first list
> when its index is in the second list:
>
> a={10,20,30,40,50,60}
>
> b={2,4,6}
>
> c=17
>
> ==>
>
> {10,20+17,30,40+17,50,60+17}
>
> I would be glad if anyone have a simple solution for that problem - I am
> convinced there is one ...
>
> Best regards
> J. Lenhardt
>
In[1]:= a = {10, 20, 30, 40, 50, 60};
b = {2, 4, 6};
c = 17;
In[4]:= ans = a;
In[5]:= ans[[b]] = ans[[b]] + c;
In[6]:= ans
Out[6]= {10, 37, 30, 57, 50, 77}
To understand this better, remember that [[.....]] is a shorthand for
Part - so look that function up.
David Bailey
http://www.dbaileyconsultancy.co.uk
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Adding value to some elements of a list Joerg Lenhardt <joerg.lenhardt@FernUni-Hagen.de> - 2013-06-05 07:15 +0000 Re: Adding value to some elements of a list David Bailey <dave@removedbailey.co.uk> - 2013-06-06 11:23 +0000 Re: Adding value to some elements of a list Kurt TeKolste <ektekolste@gmail.com> - 2013-06-06 11:50 +0000
csiph-web