I had a conversation about this issue in the mailing list several months ago: 
in short, if the spacings are regular you can do what you want. either:

a[1:4:2,1:4:2] += 100

or:

ind = slice(1,4,2)
a[ind, ind] += 100

  Nadav


-----הודעה מקורית-----
מאת: [EMAIL PROTECTED] בשם Brian Blais
נשלח: ש 26-יולי-08 16:35
אל: Discussion of Numerical Python
נושא: [Numpy-discussion] indexing (compared to matlab)
 
Hello,

I wanted to do the following thing that I do in Matlab (on a bigger  
problem), setting the values of a part of a matrix with indexing:

 >> a=floor(rand(5,5)*10)  % make an example matrix to work on

a =

      2     4     7     9     8
      6     9     2     5     2
      6     3     5     1     8
      1     5     6     1     2
      1     2     8     2     9

 >> ind=[2,4]

ind =

      2     4

 >> a(ind,ind)=a(ind,ind)+100

a =

      2     4     7     9     8
      6   109     2   105     2
      6     3     5     1     8
      1   105     6   101     2
      1     2     8     2     9


===========================

In numpy, the same gives:

In [11]:a=floor(random.rand(5,5)*10)
In [14]:a
Out[14]:
array([[ 7.,  7.,  8.,  1.,  9.],
        [ 0.,  4.,  9.,  0.,  5.],
        [ 4.,  3.,  7.,  8.,  3.],
        [ 2.,  0.,  4.,  2.,  4.],
        [ 9.,  5.,  0.,  9.,  9.]])

In [15]:ind=[1,3]

In [20]:a[ind,ind]+=100

In [21]:a
Out[21]:
array([[   7.,    7.,    8.,    1.,    9.],
        [   0.,  104.,    9.,    0.,    5.],
        [   4.,    3.,    7.,    8.,    3.],
        [   2.,    0.,    4.,  102.,    4.],
        [   9.,    5.,    0.,    9.,    9.]])

which only replaces 2 values, not all the values in the row,col  
combinations of [1,1],[1,2],etc...[3,3] like matlab.  Is there a  
preferred way to do this, which I think should be fairly common.  If  
I know that the indices are regular (like a slice) is there a way to  
do this?


                        thanks,


                                Brian Blais



-- 
Brian Blais
[EMAIL PROTECTED]
http://web.bryant.edu/~bblais




<<winmail.dat>>

_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to