3

Scipy has different kind of matrices. Two of them are the column sparse matrix and the row sparse matrix. The column sparse matrix supports fast column slicing operations and the row sparse matrix supports fast row slicing operations.

But I am not if the operation a[i,:] is a column or a row slicing operation. Any help?

Jadiel de Armas
  • 8,405
  • 7
  • 46
  • 62

1 Answers1

4

There is nothing like trying it by yourself:

In [1]: import numpy as np

In [2]: np.arange(9).reshape(3,3)
Out[2]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

In [3]: a = np.arange(9).reshape(3,3)

In [4]: a[0, :]
Out[4]: array([0, 1, 2])

In [5]: a[:, 0]
Out[5]: array([0, 3, 6])

Ergo, the first index corresponds to the row and the second to the column. a[i, :] is selecting the row i, so it is a row slicing operation.

Davidmh
  • 3,797
  • 18
  • 35
  • Your answer uses `numpy` arrays, the OP is asking `scipy.sparse` matrices. The try-it-yourself suggestion is valid, though. `csr_matrix` is faster when indexing rows, `csc_matric` when indexing columns. But either can be index either way (at least in new enough versions). – hpaulj Jun 04 '14 at 19:55
  • 1
    @hpaulj they are different objects with the same basic semantics. The question is about which one is which (and I have to check it every time). – Davidmh Jun 04 '14 at 19:59