Re: Can't apply Differences[] to a SparseArray[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg99380] Re: Can't apply Differences[] to a SparseArray[]?
- From: dh <dh at metrohm.com>
- Date: Mon, 4 May 2009 06:01:34 -0400 (EDT)
- References: <gth5k4$f2k$1@smc.vnet.net>
Hi,
I can only guess why Differences does not work with sparse arrays. The
reason may be that Differences needs neighboring elements.
A solution may be to extract the rules from a sparse array and change the
rule accordingly. Here is an example:
============================
s = SparseArray[{5, 6, 7, 10^6} -> {10, 11, 12, 13}]
r = ArrayRules[s];
r /. HoldPattern[{i1_Integer} -> x1_] :> ({i1} -> x1 - s[[i1 - 1]])
==============================
Daniel
jmay wrote:
> Mathematica newbie here.
>
> 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.
>
> Thanks,
> -Jason
>