On 6/21/06, <b class="gmail_sendername">Erin Sheldon</b> <<a href="mailto:erin.sheldon@gmail.com">erin.sheldon@gmail.com</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 6/20/06, Bill Baxter <<a href="mailto:wbaxter@gmail.com">wbaxter@gmail.com</a>> wrote:<br>> I think that one's on the NumPy for Matlab users, no?<br>><br>> <a href="http://www.scipy.org/NumPy_for_Matlab_Users">
http://www.scipy.org/NumPy_for_Matlab_Users</a><br>><br>> >>> import numpy as num<br>>  >>> a = num.arange (10).reshape(2,5)<br>> >>> a<br>> array([[0, 1, 2, 3, 4],<br>>        [5, 6, 7, 8, 9]])
<br>> >>> v = num.rand(5)<br>> >>> v<br>> array([ 0.10934855,  0.55719644,  0.7044047 ,  0.19250088,  0.94636972])<br>>  >>> num.where(v>0.5)<br>> (array([1, 2, 4]),)<br>> >>> a[:,
num.where(v>0.5)]<br>> array([[[1, 2, 4]],<br>><br>>        [[6, 7, 9]]])<br>><br>> Seems it grows an extra set of brackets for some reason.  Squeeze will get<br>> rid of them.<br>><br>> >>> a[:,
num.where(v>0.5)].squeeze()<br>> array([[1, 2, 4],<br>>        [6, 7, 9]])<br>><br>> Not sure why the squeeze is needed.  Maybe there's a better way.<br><br>where returns a tuple of arrays.  This can have unexpected results
<br>so you need to grab what you want explicitly:<br><br>>>> (w,) = num.where(v>0.5)<br>>>> a[:,w]<br>array([[1, 2, 4],<br>       [6, 7, 9]])<br></blockquote></div><br>Ah, yeh, that makes sense.  Thanks for the explanation.  So to turn it back into a one-liner you just need:
<br><br clear="all">>>> a[:,num.where(v>0.5)[0]]<br>array([[1, 2, 4],<br>       [6, 7, 9]])<br><br>I'll put that up on the Matlab->Numpy page.<br><br>--bb<br>