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


Groups > comp.soft-sys.math.mathematica > #14746 > unrolled thread

Adding value to some elements of a list

Started byJoerg Lenhardt <joerg.lenhardt@FernUni-Hagen.de>
First post2013-06-05 07:15 +0000
Last post2013-06-06 11:50 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#14746 — Adding value to some elements of a list

FromJoerg Lenhardt <joerg.lenhardt@FernUni-Hagen.de>
Date2013-06-05 07:15 +0000
SubjectAdding value to some elements of a list
Message-ID<komoi3$1un$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#14764

FromDavid Bailey <dave@removedbailey.co.uk>
Date2013-06-06 11:23 +0000
Message-ID<koprfk$cgn$1@smc.vnet.net>
In reply to#14746
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

[toc] | [prev] | [next] | [standalone]


#14766

FromKurt TeKolste <ektekolste@gmail.com>
Date2013-06-06 11:50 +0000
Message-ID<kopt23$da1$1@smc.vnet.net>
In reply to#14746
I am afraid that the answer is much easier than so far offered.

Setup
In[8]:= a=10 Range[6]
Out[8]= {10,20,30,40,50,60}
In[9]:= b=2Range[3]
Out[9]= {2,4,6}

Here is what you are looking for
In[10]:= a[[b]]+=17
Out[10]= {37,57,77}


Check resultIn[11]:= a
Out[11]= {10,37,30,57,50,77}

Kurt

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web