Reputation: 5681
i have this :
npoints=10
vectorpoint=random.uniform(-1,1,[1,2])
experiment=random.uniform(-1,1,[npoints,2])
and now i want to create an array with dimensions [1,npoints]. I can't think how to do this. For example table=[1,npoints]
Also, i want to evaluate this:
for i in range(1,npoints):
if experiment[i,0]**2+experiment[i,1]**2 >1:
table[i]=0
else:
table[i]=1
I am trying to evaluate the experiment[:,0]**2+experiment[:,1]**2
and if it is >1 then an element in table becomes 0 else becomes 1.
The table must give me sth like [1,1,1,1,0,1,0,1,1,0]. I can't try it because i can't create the array "table". Also,if there is a better way (with list comprehensions) to produce this..
Thanks!
Upvotes: 2
Views: 202
Reputation: 30581
Try:
table = (experiment[:,0]**2 + experiment[:,1]**2 <= 1).astype(int)
You can leave off the astype(int)
call if you're happy with an array of booleans rather than an array of integers. As Joe Kington points out, this can be simplified to:
table = 1 - (experiment**2).sum(axis=1).astype(int)
If you really need to create the table
array up front, you could do:
table = zeros(npoints, dtype=int)
(assuming that you've already import zeros
from numpy). Then your for loop should work as written.
Aside: I suspect that you want range(npoints)
rather than range(1, npoints)
in your for
statement.
Edit: just noticed that I had the 1s and 0s backwards. Now fixed.
Upvotes: 2