Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

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

Search the Archive

Re: Sparse Array Question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg130585] Re: Sparse Array Question
  • From: Sseziwa Mukasa <mukasa at gmail.com>
  • Date: Wed, 24 Apr 2013 07:02:14 -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: <20130423040424.702866B1B@smc.vnet.net>

One idea:

#[[1, 1]] -> Total[#[[All, 2]]] & /@ Split[Sort[myList], #[[1]] == #2[[1]] &]

In versions 7 and later you can do:

#[[1, 1]] -> Total[#[[All, 2]]] & /@ GatherBy[myList, #[[1]] &]

On Apr 23, 2013, at 12:04 AM, christopher arthur <chris.arthur1 at gmail.com> wrote:

> 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
>
> Cheers,
>
> Chris
>




  • Prev by Date: Re: Converting a magnified image of a line to a single
  • Next by Date: BibTeX support in version 9
  • Previous by thread: Re: Sparse Array Question
  • Next by thread: Re: Sparse Array Question