Re: Mathematica SparseArray Access Non-Default Values
- To: mathgroup at smc.vnet.net
- Subject: [mg108373] Re: Mathematica SparseArray Access Non-Default Values
- From: Raffy <adraffy at gmail.com>
- Date: Mon, 15 Mar 2010 05:01:48 -0500 (EST)
- References: <hnkf9j$pnd$1@smc.vnet.net>
On Mar 14, 10:06 pm, wima <t... at gmx.de> wrote:
> Hi,
>
> I am currently dealing with really huge (~ 80,000 x 80,000) sparse matrices. I this context I need to read the number of non-default values in each row.
> When Accessing the i-th row of a SparseArray, this information is given.
> E.g.
> test = SparseArray[{{1, 1} -> 3, {1, 5} -> 2, {5, 3} -> 1}];
> test[[1]]
> returns SparseArray[<2>, {5}]
> However, I fail to access the number of entries (here 2) to assign it to a variable.
> I did not find any information in the documentation so far.
> Does anyone have an idea?
>
> Thx
SparseArray has some interesting behavior since it needs to work like
a List and be accessed by Part, etc...
You can see the internals of SparseArray with:
sparseArray /. HoldPattern[SparseArray[x___]] :> {x}
You can get the number of non-default values by doing:
sparseArray /. SparseArray[_, _, _, x_] :> Length@Last[x]