Re: Sparse Array Question
- To: mathgroup at smc.vnet.net
- Subject: [mg130595] Re: Sparse Array Question
- From: Szabolcs HorvÃt <szhorvat at gmail.com>
- Date: Thu, 25 Apr 2013 02:51:44 -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
- References: <kl7d3o$kdc$1@smc.vnet.net>
On 2013-04-24 01:39:04 +0000, christopher arthur said: > Hello, > > I have a list of rules for defining a SparseArray. The list has some > coordinates repeated with different values. If a coordinate appears in > the list more than once, I want to sum up the values, and then have a > SparseArray > > i.e., if my list were myList={{1,1}->1,{2,1}->2,{1,1}->3} then my > SparseArray should have {1,1}->4. I've tried using ReplacePart on an > array to say myArray=ReplacePart[myArray,...] but this seems horribly > inefficient as a way to change values in an array. > > I'm using Mathematica 5.2 There is a system option for this. Hopefully it works in 5.2 too, but I cannot test. In[1]:= Normal@SparseArray[{{1, 1} -> 1, {2, 1} -> 2, {1, 1} -> 3}] Out[1]= {{1}, {2}} In[2]:= SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Total] In[3]:= Normal@SparseArray[{{1, 1} -> 1, {2, 1} -> 2, {1, 1} -> 3}] Out[3]= {{4}, {2}} In[4]:= (* set it back to the default *) SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> First] Be very very careful with this! It modified global behaviour, so there's no telling what other indirect effects it will have.