Reputation: 47
So i have this code writen in python. I'm not going to explain it as it is a simple syntax fix that i can't seem to see so it is useless to explain what's for.
The problem that i have is that for a given d, for example 15 i get the value of "cuentas" right and "e" correct.
What i want to do is to iterate through a set of d's and get the value of each cuentas, and each e in order to plot d vs e.
My problem is that i dont seem to get how to create a matrix in python.
In matlab i used to write two different loops like theese:
for i=1:1:N
for j=1:9
a[i,j]= and so on
a[i,j] would be a matrix with N rows and 9 columns that i could access and manipulate.
In my code below i will intentionaly put # comments where i want to iterate through the distances
import numpy as np
import matplotlib.pyplot as plt
N=100000
cos=np.zeros(N)
phi=np.zeros(N)
teta=np.zeros(N)
a=np.zeros(N)
xrn=np.zeros(N)
yrn=np.zeros(N)
zrn=np.zeros(N)
x=np.zeros(N)
y=np.zeros(N)
z=np.zeros(N)
lim1=14.7
lim2=3.35
lim3=-lim1
lim4=-lim2
#d=np.array([15,20,25,30,35,40,45,50,55])
d=15
#for j in range(9):
for i in range(N):
cos[i]=np.random.uniform(-1,1)
teta[i]=np.random.uniform(-np.pi,np.pi)
phi[i]=np.random.uniform(0,2*np.pi)
# a[i]=d[j]/cos[i]*np.cos(phi[i])
a[i]=d/cos[i]*np.cos(phi[i])
xrn[i]=np.random.uniform(-1,1)*lim1
yrn[i]=np.random.uniform(-1,1)*lim2
x[i]=a[i]*np.sin(teta[i])*np.cos(phi[i])+xrn[i]
y[i]=a[i]*np.sin(teta[i])*np.sin(phi[i])+yrn[i]
#cuentas[j]=0
cuentas=0
#for j in range(9):
for i in range(N):
if a[i]>0 and x[i] < lim1 and x[i]>lim3 and y[i] < lim2 and y[i]>lim4:
cuentas=cuentas+1
#e[j]=cuenta[j]/N
e=cuentas/N
Thanks so much to those even reading!!
Upvotes: 0
Views: 112
Reputation: 47
So i've adopted your answers and it worked!
If anyone wonders it is a Monte Carlo simulation of how many particles would pass through both ractangular detectors. As i throw the particles from detector 1 it is trivial they go through it, and i count the number that pass detector 2.
The corrected code is
N=100000
"La dirección viene dada por v=[rsin(teta)*cos(phi),rsin(teta)sin(phi),rcos(teta)]"
"Los vectores que vamos a usar debemos inicializarlos como un vector de ceros"
cos=np.zeros([10,N])
phi=np.zeros([10,N])
teta=np.zeros([10,N])
a=np.zeros([10,N])
xrn=np.zeros(N)
yrn=np.zeros(N)
zrn=np.zeros(N)
x=np.zeros([10,N])
y=np.zeros([10,N])
z=np.zeros([10,N])
lim1=14.7
lim2=3.35
lim3=-lim1
lim4=-lim2
"d son las disversas distancias a las que colocamos la fuente con respecto al detector"
d=np.array([0.00001,15,20,25,30,35,40,45,50,55])
"e es la eficiencia geométrica simulada"
e=np.zeros(10)
"Debemos definir el coseno como números aleatorios en vez de el ángulo teta, debido a que queremos"
"que se distribuyan uniformemente por toda la esfera"
for j in range(10):
for i in range(N):
cos[j,i]=np.random.uniform(0,1)
phi[j,i]=np.random.uniform(0,2*np.pi)
a[j,i]=d[j]/cos[j,i]
xrn[i]=np.random.uniform(-1,1)*lim1
yrn[i]=np.random.uniform(-1,1)*lim2
x[j,i]=a[j,i]*np.sin(math.acos(cos[j,i]))*np.cos(phi[j,i])+xrn[i]
y[j,i]=a[j,i]*np.sin(math.acos(cos[j,i]))*np.sin(phi[j,i])+yrn[i]
cuentas=np.zeros(10)
for j in range(10):
for i in range(N):
if a[j,i]>0 and x[j,i] < lim1 and x[j,i]>lim3 and y[j,i] < lim2 and y[j,i]>lim4:
cuentas[j]=cuentas[j]+1
e[j]=cuentas[j]/N
thanks to everyone!
Upvotes: 0
Reputation: 19624
You can create matrix in python using numpy in the following way:
n=5
k=4
a=np.zeros([n,k])
for i in range(n):
for j in range(k):
a[i,j]=i+j
print(a)
The result is
[[ 0. 1. 2. 3.]
[ 1. 2. 3. 4.]
[ 2. 3. 4. 5.]
[ 3. 4. 5. 6.]
[ 4. 5. 6. 7.]]
Upvotes: 1
Reputation: 10318
Short version:
This is the exact equivalent of your MATLAB code in Python
a = np.zeros([N, 9])
for i in range(N):
for j in range(9):
a[i,j]= and so on
The only major difference is that you need to define the array beforehand, which you really should be doing in MATLAB as well if you want your code to have reasonable performance.
However, if you don't know the size beforehand, you can use lists in Python, then convert to a numpy array at the end. This will be much, much faster than your MATLAB example for large arrays because of the internals of how lists and matrices/arrays are handled:
a = []
for i in range(N):
a.append([])
for j in range(9):
a[-1].append( and so on
a = np.array(a)
The [-1]
means (the "last element of a
", and the append
puts whatever is inside the parentheses at the end of the list. So. a[-1].append(foo)
means "put foo
inside whatever is in the last element of a
.
Long version:
Your MATLAB code will work in Python roughly the same way, but there are a few notable differences you need to take into account.
First, assigning to an index larger than an existing array/matrix works in MATLAB but not in numpy. So if you have a size [5, 5]
array/matrix, in MATLAB you can assign to element [5, 6]
, but you can't in numpy. This means in MATLAB you can start with an empty array while in numpy you have to set the array size beforehand. Note that MATLAB matrices can't actually be resized, you are actually making a new matrix each time through the loop and copying all the data to it. This is very slow, which is why MATLAB warns you to pre-allocate the array. Numpy just doesn't pretend to be able to resize arrays, so you need to be more explicit about doing the copy, pre-allocate, or use a list (which is resizable).
Second, similarly, MATLAB doesn't require you define the matrix before you use it, while numpy does. The reason for this is because traditionally MATLAB has three data structures (matrices, cell arrays, and structs), each with their own style of indexing. So MATLAB can figure out what sort of data structure you want to create just from how you index it. Python only has one style of indexing, so it can't make this sort of guess.
Third, using a single array size in MATLAB with some (but not all) fucntions creates a 2D square matrix with each dimension of that size, while in numpy it creates a 1D array. I am not sure from your code if that is what you are expecting or not. Frankly I have no idea why MATLAB works this way.
Fourth, numpy arrays can have any number of dimensions, 0 (scalars), 1 (vectors), 2, 3, 4, etc. MATLAB matrices, on the other hand, must have at least two dimensions. This can cause some unexpected differences, like transposing doing nothing for numpy vectors.
As for your Python code, without you saying what is going wrong I can't tell you how to fix it. But hopefully I have given you enough info to do it yourself.
Upvotes: 1