<p dir="ltr">Try:</p>
<p dir="ltr">np.argsort(a)[-n:]</p>
<p dir="ltr">-n</p>
<div class="gmail_quote">On 18 Aug 2013 10:10, "Sudheer Joseph" <<a href="mailto:sudheer.joseph@yahoo.com">sudheer.joseph@yahoo.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div style="font-size:12pt;font-family:Courier New,courier,monaco,monospace,sans-serif"><div><span><br></span></div><div> </div><br>However, I was looking for the indices of the biggest 3 numbers which can be used to find corresponding values in another vector, which made me to write the function. Is there a smarter way for getting indices?<br>
with best regards,<br>Sudheer<br>Thanks a lot Eric,<br><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;margin-top:5px;padding-left:5px"><div style="font-family:Courier New,courier,monaco,monospace,sans-serif;font-size:12pt">
<div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div><div><div><div style="font-size:12pt;font-family:Courier New,courier,monaco,monospace,sans-serif">                    Here comes the smartness of python!!, I am just climbing the bottom steps...<br>
with best regards,<br>Sudheer<br><div><span><br></span></div><div> </div><div>***************************************************************<br>Sudheer Joseph         <br>Indian National Centre for Ocean Information Services<br>
Ministry of Earth Sciences, Govt. of India<br>POST BOX NO: 21, IDA Jeedeemetla P.O.<br>Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55<br>Tel:<a href="tel:%2B91-40-23886047" value="+914023886047" target="_blank">+91-40-23886047</a>(O),Fax:<a href="tel:%2B91-40-23895011" value="+914023895011" target="_blank">+91-40-23895011</a>(O),<br>
Tel:<a href="tel:%2B91-40-23044600" value="+914023044600" target="_blank">+91-40-23044600</a>(R),Tel:+91-40-9440832534(Mobile)<br><a href="mailto:E-mail%3Asjo.India@gmail.com" target="_blank">E-mail:sjo.India@gmail.com</a>;<a href="mailto:sudheer.joseph@yahoo.com" target="_blank">sudheer.joseph@yahoo.com</a><br>
Web-
 <a href="http://oppamthadathil.tripod.com" target="_blank">http://oppamthadathil.tripod.com</a><br>***************************************************************</div><div><br><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;margin-top:5px;padding-left:5px">
  <div style="font-family:Courier New,courier,monaco,monospace,sans-serif;font-size:12pt"> <div style="font-family:times new roman,new york,times,serif;font-size:12pt"> <div dir="ltr"> <hr size="1">  <font face="Arial"> <b><span style="font-weight:bold">From:</span></b> Eric Firing <<a href="mailto:efiring@hawaii.edu" target="_blank">efiring@hawaii.edu</a>><br>
 <b><span style="font-weight:bold">To:</span></b> <a href="mailto:numpy-discussion@scipy.org" target="_blank">numpy-discussion@scipy.org</a> <br> <b><span style="font-weight:bold">Sent:</span></b> Sunday, 18 August 2013 1:57 PM<br>
 <b><span style="font-weight:bold">Subject:</span></b> Re: [Numpy-discussion] strange behavior of variable<br> </font> </div> <div><br>On 2013/08/17 9:49 PM, Sudheer Joseph wrote:<br>>
 Hi,<br>>           I have defined a small function to find the n maximum values<br>> of an array as below. With in it I assign the input array to a second<br>> array and temporarily make the array location after first iteration as<br>
> nan. I expected this temporary change to be limited to the second<br>> variable. However my initial variable gets modified. Can any one through<br>> some light to what is happening here?. In case of matlab this logic works.<br>
><br>> ######<br>> #FUNCTION maxn<br>> ######<br>> import numpy as np<br>> def max_n(a,n):<br>>       b=a<br><br>This is not making "b" a copy of "a", it is simply making it an alias <br>
for it.  To make it a copy you could use "b = a[:]", or "b = a.copy()"<br><br>It sounds like you don't really need a function, however.  Try this:<br><br># test data:<br>a = np.random.randn(10)<br>
n = 2<br><br># One-line
 solution:<br>biggest_n = np.sort(a)[-n:]<br><br>print a<br>print biggest_n<br><br>If you want them ordered from largest to smallest, just reverse the list:<br><br>biggest_n = biggest_n[::-1]<br><br>Eric<br><br><br>>       result=[]<br>
>       for i in np.arange(1,n+1):<br>>           mxidx=np.where(b==max(b))<br>>           result.append(mxidx)<br>>           b[mxidx]=np.nan<br>>       result=np.ravel(result)<br>>       return(result)<br>
><br>> ### TEST<br>> In [8]: x=np.arange(float(0),10)<br>><br>> In [9]: max<br>> max    max_n<br>><br>> In [9]: max_n(x,2)<br>> Out[9]: array([9, 8])<br>><br>> In [10]: x<br>> Out[10]: array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,  nan,  nan])<br>
>
 ***************************************************************<br>> Sudheer Joseph<br>> Indian National Centre for Ocean Information Services<br>> Ministry of Earth Sciences, Govt. of India<br>> POST BOX NO: 21, IDA Jeedeemetla P.O.<br>
> Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55<br>> Tel:<a href="tel:%2B91-40-23886047" value="+914023886047" target="_blank">+91-40-23886047</a>(O),Fax:<a href="tel:%2B91-40-23895011" value="+914023895011" target="_blank">+91-40-23895011</a>(O),<br>
> Tel:<a href="tel:%2B91-40-23044600" value="+914023044600" target="_blank">+91-40-23044600</a>(R),Tel:+91-40-9440832534(Mobile)<br>> E-mail:<a rel="nofollow" href="mailto:sjo.India@gmail.com" target="_blank">sjo.India@gmail.com</a>;<a rel="nofollow" href="mailto:sudheer.joseph@yahoo.com" target="_blank">sudheer.joseph@yahoo.com</a><br>
> Web- <a rel="nofollow" href="http://oppamthadathil.tripod.com/" target="_blank">http://oppamthadathil.tripod.com</a><br>> ***************************************************************<br>><br>><br>>
 _______________________________________________<br>> NumPy-Discussion mailing list<br>> <a rel="nofollow" href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>> <a rel="nofollow" href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
><br><br>_______________________________________________<br>NumPy-Discussion mailing list<br><a rel="nofollow" href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br><a rel="nofollow" href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br><br></div> </div> </div> </blockquote></div>   </div></div></div><br>_______________________________________________<br>NumPy-Discussion
 mailing list<br><a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br><a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br><br></div> </div> </div> </blockquote></div>   </div></div><br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div>