# [SciPy-User] Matrix indexing and updating

Emanuele Olivetti emanuele@relativita....
Sun Oct 31 15:21:07 CDT 2010

```Hi Luca,

If I understand you problem correctly, maybe this example can help you:

----
In [1]: import numpy as np

In [2]: mat = np.arange(100).reshape(10,10)

In [3]: rows = [[1],[3],[4]]

In [4]: columns = [4,5,9]

In [5]: mat[rows,columns]
Out[5]:
array([[14, 15, 19],
[34, 35, 39],
[44, 45, 49]])
In [6]: mat[rows,columns]=1

In [7]: mat
Out[7]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
[10, 11, 12, 13,  1,  1, 16, 17, 18,  1],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33,  1,  1, 36, 37, 38,  1],
[40, 41, 42, 43,  1,  1, 46, 47, 48,  1],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])

----

Best,

Emanuele

On 10/31/2010 04:03 PM, Luca Manini wrote:
>      Hi everybody,
>
>      I'm new to this list and here comes my first question about matrix
>      indexing and updating (in scipy, of course).
>
>      I'm starting writing some code (for my wife) to solve finite elements
>      problems.  One typical action is to access (both for reading and
>      writing) som "sparse" submatrices of a given BIG matrix.  The
>      submatrices are defined by two lists of indices, one for the rows and
>      one for the cols.
>
>      I've two problems with that.
>
>      The first one is how to ACCESS (read) the matrix entries from two
>      lists of indices, in "pseudo code":
>
>          mat = scipy.matrix( range(0,100) )
>          mat( [1,3,4], [4,5,9] )
>
>      in order to get all the elements mat[i,j] for i in [1,3,4] and j in
>      [4,5,9].
>
>      The second is how to CHANGE those elements.  I manage to solve the
>      first problem (but the sintax seems to me "uglier than needed") but
>      not the second.
>
>

```