Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

incrementing elements of a SparseArray

72 views
Skip to first unread message

Matt Enlow

unread,
Dec 15, 2011, 5:04:29 AM12/15/11
to
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

Leonid Shifrin

unread,
Dec 16, 2011, 5:55:29 AM12/16/11
to
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

DrMajorBob

unread,
Dec 16, 2011, 6:03:52 AM12/16/11
to
Initialization:

Clear[m]
m[i_,j_]=0;

Setting a non-zero element:

m[10,5] = Pi

Retrieval in matrix form:

Array[m, {10, 10}]

{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0,
0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0,
0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0,
0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0,
0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, \[Pi], 0, 0, 0, 0, 0}}

Bobby

On Thu, 15 Dec 2011 03:55:37 -0600, Matt Enlow <matt...@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


--
DrMaj...@yahoo.com

0 new messages