[SciPy-user] Would like to simplify my 3 where statements

David Grant davidgrant at gmail.com
Fri Jul 21 10:47:05 EDT 2006


On 7/20/06, Michael Sorich <michael.sorich at gmail.com> wrote:
>
> If I run the script below I get [0,2]. Is this what you want? In any
> case the code you wrote looks fairly simple now. An alternative
> function to where is nonzero, however this will not make and
> difference.
>
> from numpy import *
> i=3
> j=4
> A = array([[0, 1, 0, 1, 0],
>       [1, 0, 1, 0, 1],
>       [1, 0, 0, 0, 0],
>       [0, 0, 1, 0, 1],
>       [1, 1, 0, 1, 0]])
> ans=where(A[:,j]==1)[0] #ans=[1,3]
> if A[i,j] == 1:
>    ans -= 1
> print ans
> >> [0 2]
>
> Oops, I screwed up. It shouldn't be ans -= 1. It should be something like
ans.remove(i). I was getting confused because previously my algorithm was
just finding the length of ans, then subtracting 1 if A[i,j] == 1. Now I
want the actual indices. Sorry for the confusion.. I wrote this algorithm a
while go and have moved on to other code...although I am still interested in
different ways to do this. So what's the quickest way to do a remove type
operation on a numpy array?

-- 
David Grant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20060721/c3be509f/attachment.html>


More information about the SciPy-User mailing list