From: wima on
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

From: Ray Koopman on
On Mar 14, 10:06 pm, wima <t...(a)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

If s is a sparse array then

s /. SparseArray[_,_,_,d_] :> Differences@d[[2,1]]

will return a list of the number of non-default values in each row.

From: Raffy on
On Mar 14, 10:06 pm, wima <t...(a)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(a)Last[x]

From: Bob Hanlon on

test = SparseArray[{
{1, 1} -> 3, {1, 5} -> 2, {5, 3} -> 1}];

Count[ArrayRules[test], {1, _} -> _]

2

Count[ArrayRules[test], {1, _}, 2]

2


Bob Hanlon

---- wima <tba2(a)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