MathGroup Archive 2011

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

Search the Archive

Re: Manipulating vectors with Position

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117377] Re: Manipulating vectors with Position
  • From: telefunkenvf14 <rgorka at gmail.com>
  • Date: Wed, 16 Mar 2011 06:30:01 -0500 (EST)
  • References: <ilnha9$obc$1@smc.vnet.net>

On Mar 15, 6:07 am, Lengyel Tamas <lt... at hszk.bme.hu> wrote:
> Dear Mathworld Users,
> I am tackling a rather straightforward problem. Here it is.
>
> 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}}
>
> I think it is simple, but I can't seem to do it.
>
> Any ideas?
>
> Thank you.
>
> Tam s

I think you meant {b+d +e} in your desired output...

In[3]:= Extract[Array1, List /@ Array2]
Out[3]= {{a}, {b, d, e}, {c, f}}

In[4]:= Extract[Total@Array1, List /@ Array2]
Out[4]= {a, b + d + e, c + f}

And if you really wanted exactly the nested list structure given in
your example:

List /@ Extract[Total@Array1, List /@ Array2]

-RG


  • Prev by Date: Re: Manipulating vectors with Position
  • Next by Date: Re: how to partition a list
  • Previous by thread: Re: Manipulating vectors with Position
  • Next by thread: Expressions as images