# [SciPy-user] Replacing elements in matrix

Dimitri D'Or dimitri.dor at fssintl.com
Mon Apr 25 04:19:44 CDT 2005

```Hello,

I come from the Matlab world and I'm used to write operations such as:
A[indexi,indexj]=A[indexi,indexj]+B
where A is a n by n matrix, indexi is i by 1 vector, indexj is a j by 1
vector and B is a i by j matrix.

With this operation, some of the elements of A are replaced by their
original value augmented by some value coming from B.

for example, take

A=array([[1,5,7,8,2],[5,3,4,6,7],[9,4,6,7,2],[0,2,0,3,4]])
indexi=array([0,2])
indexj=array([1,2,4])
B=array([[10,20,30],[100,200,300]])

With this, Matlab results would be
A[indexi,indexj]=array([[5, 7, 2],[4, 6, 2]])
and
A[indexi,indexj]=A[indexi,indexj]+B yields
A=array([[1,15,27,8,32],[5,3,4,6,7],[9,104,206,7,302],[0,2,0,3,4]])

I would like to make the same operation with Python. Would you please
propose me a compact code for achieving that?

Thank you,

Dimitri

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.scipy.net/pipermail/scipy-user/attachments/20050425/399488be/attachment.htm
```