[Numpy-discussion] strange behavior of variable
Sudheer Joseph
sudheer.joseph at yahoo.com
Sun Aug 18 03:49:09 EDT 2013
Hi,
I have defined a small function to find the n maximum values of an array as below. With in it I assign the input array to a second array and temporarily make the array location after first iteration as nan. I expected this temporary change to be limited to the second variable. However my initial variable gets modified. Can any one through some light to what is happening here?. In case of matlab this logic works.
######
#FUNCTION maxn
######
import numpy as np
def max_n(a,n):
b=a
result=[]
for i in np.arange(1,n+1):
mxidx=np.where(b==max(b))
result.append(mxidx)
b[mxidx]=np.nan
result=np.ravel(result)
return(result)
### TEST
In [8]: x=np.arange(float(0),10)
In [9]: max
max max_n
In [9]: max_n(x,2)
Out[9]: array([9, 8])
In [10]: x
Out[10]: array([ 0., 1., 2., 3., 4., 5., 6., 7., nan, nan])
***************************************************************
Sudheer Joseph
Indian National Centre for Ocean Information Services
Ministry of Earth Sciences, Govt. of India
POST BOX NO: 21, IDA Jeedeemetla P.O.
Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55
Tel:+91-40-23886047(O),Fax:+91-40-23895011(O),
Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile)
E-mail:sjo.India at gmail.com;sudheer.joseph at yahoo.com
Web- http://oppamthadathil.tripod.com
***************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130818/141f13fc/attachment.html>
More information about the NumPy-Discussion
mailing list