Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7258 > unrolled thread

Re: incrementing elements of a SparseArray

Started byLeonid Shifrin <lshifr@gmail.com>
First post2011-12-16 10:55 +0000
Last post2011-12-16 10:55 +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.


Contents

  Re: incrementing elements of a SparseArray Leonid Shifrin <lshifr@gmail.com> - 2011-12-16 10:55 +0000

#7258 — Re: incrementing elements of a SparseArray

FromLeonid Shifrin <lshifr@gmail.com>
Date2011-12-16 10:55 +0000
SubjectRe: incrementing elements of a SparseArray
Message-ID<jcf831$6rn$1@smc.vnet.net>
Why don't you assign your original SparseArray to a symbol/variable (say
s), and then
increment that variable, properly indexed (e.g. s[[1,1]]++). Note however
that this will be
efficient only if you end up with not too many incremented elements, due to
the way
SparseArray is implemented (it uses packed arrays to store  positions of
non-zero elements, and every time you convert some element from zero to
non-zero,
that packed array has to be modified (recreated)). But if you final array
will be
mostly zeros, then no need to worry.

Regards,
Leonid


On Thu, Dec 15, 2011 at 12:55 PM, Matt Enlow <mattenlow@gmail.com> wrote:

> Hi,
>
> I am creating a very large 2-dimensional SparseArray ("M") by starting with
> an empty one (all zeros) and iterating a tight loop, which increments a
> different element of M each time. In the end result, M will still be mostly
> zeros, but some entries will be anywhere from 1 to about 50. I have a way
> to do it, but I feel as though there is probably a better way.
>
> Originally I thought I could use MapAt, but when I use it on a SparseArray,
> it returns a "Normal" array, which I don't want to keep having to convert
> back to a SparseArray every time. My "fix" was to create a new SparseArray
> that only has a 1 in the position I want to increment (zeros everywhere
> else), then add it to M. Again, this works, but feels inefficient. Is there
> a simpler and/or more efficient way to do this?
>
> Thanks in advance,
>
> Matt Enlow
>

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web