[SciPy-user] more help with sparse matrix: creating

Stefan van der Walt stefan@sun.ac...
Mon Oct 8 15:19:59 CDT 2007


On Mon, Oct 08, 2007 at 06:33:57PM +0100, Robin wrote:
> In this case I know the nnz exactly in advance, which is why I was keen to
> preallocate.
> 
> Unfortunately it seems I still can't do the MATLAB style indexing I wanted,
> something like
> M[2, [1,2,3]] = 1
> but since I will be updating row by row anyway I can get around this by
> creating a temporary row vector to insert with slicing.

In scipy's lil_matrix, the structure is grown dynamically.  You can
do:

In [61]: z = sp.lil_matrix((4,4))

In [62]: z[2,[1,2,3]] = 1

In [63]: z.todense()
Out[63]: 
matrix([[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  1.,  1.,  1.],
        [ 0.,  0.,  0.,  0.]])

Cheers
Stéfan


More information about the SciPy-user mailing list