Re: Can't apply Differencesto a SparseArray[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg99345] Re: Can't apply Differencesto a SparseArray[]?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sun, 3 May 2009 05:22:38 -0400 (EDT)
On 5/2/09 at 6:01 AM, jason.may at gmail.com (jmay) wrote:
>I'd like to apply the Differences[] function to a sparse array.
>Other list operations appear to work, but I'm seeing this:
>s = SparseArray[{5, 6, 7} -> {10, 11, 12}]
>Differences[s]
>Differences::list: List expected at position 1 in Differences
>[SparseArray[Automatic,{7},0,{1,{{0,3},{{5},{6},{7}}},{10,11,12}}]].
>What is special about Differences? I don't want to convert the
>sparse array to a regular list; my data set could have thousands or
>millions of missing elements.
Obviously Differences doesn't accept SparseArray arguments. But
you can get the differences of successive elements without
converting the array to a normal list. For example,
In[24]:= s = SparseArray[{5, 6, 7} -> {10, 11, 12}];
d = Rest[s] - Most[s]
Out[25]= SparseArray[<3>,{6}]
In[26]:= Normal[d]
Out[26]= {0,0,0,10,1,1}