Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14284 > unrolled thread
| Started by | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| First post | 2013-04-24 11:01 +0000 |
| Last post | 2013-04-24 11:01 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Sparse Array Question Bob Hanlon <hanlonr357@gmail.com> - 2013-04-24 11:01 +0000
| From | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| Date | 2013-04-24 11:01 +0000 |
| Subject | Re: Sparse Array Question |
| Message-ID | <kl8e32$olt$1@smc.vnet.net> |
myList = {{1, 1} -> 1, {2, 1} -> 2,
{1, 1} -> 3, {2, 2} -> 1, {1, 1} -> 1};
myList //.
{s___, a_ -> b_, m___, a_ -> c_, e___} ->
{s, a -> (b + c), m, e}
{{1, 1} -> 5, {2, 1} -> 2, {2, 2} -> 1}
For v3 or later (Split was new in v3)
#[[1, 1]] -> Plus @@ #[[All, -1]] & /@
Split[Sort[myList], #1[[1]] == #2[[1]] &]
{{1, 1} -> 5, {2, 1} -> 2, {2, 2} -> 1}
For v5 or later (Total was new in v5)
#[[1, 1]] -> Total[#[[All, -1]]] & /@
Split[Sort[myList], #1[[1]] == #2[[1]] &]
{{1, 1} -> 5, {2, 1} -> 2, {2, 2} -> 1}
For v7 or later (GatherBy was new in v7)
#[[1, 1]] -> Total[#[[All, -1]]] & /@
GatherBy[myList, First]
{{1, 1} -> 5, {2, 1} -> 2, {2, 2} -> 1}
Bob Hanlon
On Tue, Apr 23, 2013 at 12:04 AM, christopher arthur <
chris.arthur1@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
>
>
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web