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


Groups > comp.soft-sys.math.mathematica > #7274

Re: incrementing elements of a SparseArray

From DrMajorBob <btreat1@austin.rr.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: incrementing elements of a SparseArray
Date 2011-12-16 11:03 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jcf8io$707$1@smc.vnet.net> (permalink)
References <201112150955.EAA22977@smc.vnet.net>

Show all headers | View raw


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 <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


-- 
DrMajorBob@yahoo.com

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: incrementing elements of a SparseArray DrMajorBob <btreat1@austin.rr.com> - 2011-12-16 11:03 +0000

csiph-web