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
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
הודעה מקורית מאת: numpydiscussionbounces@scipy.org בשם Brian Blais נשלח: ש 26יולי08 16:35 אל: Discussion of Numerical Python נושא: [Numpydiscussion] 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
This is probably the most asked single question. Use ``ix_``. Example below.
Cheers, Alan Isaac
import numpy as np a=np.floor(np.random.rand(5,5)*10) ind=[1,3] a[np.ix_(ind,ind)]+=100 a
array([[ 9., 1., 2., 8., 5.], [ 2., 102., 7., 109., 0.], [ 8., 0., 2., 2., 2.], [ 1., 103., 5., 101., 7.], [ 1., 4., 7., 2., 3.]])
On Jul 26, 2008, at Jul 26:10:12 AM, Alan G Isaac wrote:
This is probably the most asked single question. Use ``ix_``. Example below.
cool. this should definitely be in the Numpy for Matlab users page, http://www.scipy.org/NumPy_for_Matlab_Users, right after the line:
Matlab Numpy Notes a(1:3,5:9) a[0:3][:,4:9] rows one to three and columns five to nine of a
because this example gives a readonly submatrix. I looked there first to get an answer, and it wasn't forthcoming.
thanks for the tip!
bb
On Sat, Jul 26, 2008 at 8:05 AM, Brian Blais bblais@bryant.edu wrote:
cool. this should definitely be in the Numpy for Matlab users page, http://www.scipy.org/NumPy_for_Matlab_Users, right after the line: Matlab Numpy Notes
By all means, please put it in, it's a wiki after all.
One of the unwritten rules of open source projects: when others take the time to help you out and there's a publicly accessible place for documenting things, the nice thing to do is to pay back the good will of the more experienced users with a public record of this information. It will help you clarify things in your head as you write them up, it will get you more involved with the community, help others in the future and earn you good karma :)
Cheers,
f
this should definitely be in the Numpy for Matlab users page, http://www.scipy.org/NumPy_for_Matlab_Users, right after the line: Matlab Numpy Notes
Good form is to make that change yourself when you get useful advice. But I did it this time.
Cheers, Alan Isaac
On Aug 18, 2008, at 12:04 , Alan G Isaac wrote:
this should definitely be in the Numpy for Matlab users page, http://www.scipy.org/NumPy_for_Matlab_Users, right after the line: Matlab Numpy Notes
I did put it in! (did it disappear or something?) I also modified the previous line to say that the syntax a[0:3][:,4:9] gave readonly access.
bb
Brian Blais wrote:
this should definitely be in the Numpy for Matlab users page, http://www.scipy.org/NumPy_for_Matlab_Users, right after the line: Matlab Numpy Notes
Brian Blais wrote:
I did put it in! (did it disappear or something?) I also modified the previous line to say that the syntax a[0:3][:,4:9] gave readonly access.
I see; I misunderstood your intended location. Hmmm. I think the partial duplication ends up being a good thing, so I'm not going to change the Notes.
Thanks, Alan
participants (4)

Alan G Isaac

Brian Blais

Fernando Perez

Nadav Horesh