incrementing elements of a SparseArray
- To: mathgroup at smc.vnet.net
- Subject: [mg123681] incrementing elements of a SparseArray
- From: Matt Enlow <mattenlow at gmail.com>
- Date: Thu, 15 Dec 2011 04:55:37 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Reply-to: mattenlow at gmail.com
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
- Follow-Ups:
- Re: incrementing elements of a SparseArray
- From: Leonid Shifrin <lshifr@gmail.com>
- Re: incrementing elements of a SparseArray
- From: DrMajorBob <btreat1@austin.rr.com>
- Re: incrementing elements of a SparseArray