Re: Manipulating vectors with Position
- To: mathgroup at smc.vnet.net
- Subject: [mg117376] Re: Manipulating vectors with Position
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 16 Mar 2011 06:29:50 -0500 (EST)
On 3/15/11 at 6:07 AM, lt648 at hszk.bme.hu (Lengyel Tamas) wrote: >1) I have generated an array of values (Array1) 2) I have generated >an array of vectors in Array2, whose Length varies, and in each >subvector the numbers contain the Position (index) of those elements >that I need from Array1. So far so good. >3) What I need is to create a code that picks out those elements >from Array 1 and ads them together if Array2's subvectors have more >than 1 element. >Here's an example to make it clear: >Array1 = {a,b,c,d,e,f}; (*a;;f are numbers*) Array2 = >{{1},{2,4,5}, {3,6}}; >My outpout should be: {{a}, {a+d+e}, {c+f}} Did you perhaps make a typo here? Based on your description I would have expected the output to be {{a}, {b+d+e}, {c+f}} since the second set of indices starts with 2 rather than 1. Assuming you did make a typo and you do want the output to be as I would expect from your description this can be done as: In[8]:= {Plus @@ array1[[#]]} & /@ array2 Out[8]= {{a}, {b + d + e}, {c + f}} or In[9]:= {Total@array1[[#]]} & /@ array2 Out[9]= {{a}, {b + d + e}, {c + f}}