block matrix assignment in Python

Viewed 1026

From this mwe:

a=np.zeros((5,5))
b=np.zeros((2,2))
a=np.matrix(a)
b=np.matrix(b)
b[0,0]=4
b[1,1]=9
b[0,1]=7
indice=[2,3]
# 1
c=a[indice,:][:,indice]
c=b
print c
# 2
a[indice,:][:,indice]=b
print a[indice,:][:,indice]

I get:

>>> c
matrix([[ 4.,  7.],
        [ 0.,  9.]])

and:

>>> a[indice,:][:,indice]
matrix([[ 0.,  0.],
        [ 0.,  0.]])

I do not understand why the values of a remain zeroes. If a similar operation is done in two steps, everything works fine:

>>> for k in range(len(indice)):
...     a[indice[k],indice]=b[k,:]

I obtain:

>>> a
matrix([[ 0.,  0.,  0.,  0.,  0.],
        [ 0.,  4.,  0.,  7.,  0.],
        [ 0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  9.,  0.],
        [ 0.,  0.,  0.,  0.,  0.]])
1 Answers
Related