Re: Efficient function to accumulate a list of {value,coord} into array

• To: mathgroup at smc.vnet.net
• Subject: [mg132509] Re: Efficient function to accumulate a list of {value,coord} into array
• From: Bill Rowe <readnews at sbcglobal.net>
• Date: Fri, 4 Apr 2014 03:56:18 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-outx@smc.vnet.net
• Delivered-to: mathgroup-newsendx@smc.vnet.net

```On 4/3/14 at 2:17 AM, julian.w.francis at gmail.com wrote:

>I am struggling with how to convert a list of (value, coords) tuples
>into an array such that the an element in the array should represent
>the sum of all value elements in that list with matching coords, and
>zero if no matches, e.g.

>{ {3.6, 7,4}, {3,4, 8,6}, {2.1, 7,4} }

I assume the second triplet was meant to be {3.4, 8, 6} rather
than what you posted. If so, use GatherBy to group common
coordinates together, map Total to the column of values,
transform them to rules and use SparseArray to create the array.
Putting that together and creating a 10x10 array would be done
as follows:

In[1]:= particles = {{3.6, 7, 4}, {3.4, 8, 6}, {2.1, 7, 4}};

In[2]:= m =
SparseArray[(#[[1, -2 ;;]] -> Total[#[[All, 1]]]) & /@
GatherBy[particles, #[[-2 ;;]] &], {10, 10}];

In[3]:= Most@ArrayRules@m

Out[3]= {{7,4}->5.7,{8,6}->3.4}

```

• Prev by Date: Parallelization sometimes a plus, sometimes not
• Next by Date: Re: Efficient function to accumulate a list of
• Previous by thread: Re: Efficient function to accumulate a list of {value,coord} into array
• Next by thread: Re: Efficient function to accumulate a list of {value,coord} into array