Erreur while et break traduction matlab à python
Bonjour, je reviens vers vous, car il y a un truc qui m'échappe dans une portion de programme que j'ai traduit. Voilà lors de ma première traduction du programme matlab ci-dessous la boucle while s'exécutait à l'infini j'ai donc mis un break pour casser la boucle et ça avait l'air de marcher, sauf qu'en fait ça modifie des variables qui se trouve dans le for. Je m'intéresse à i. J'ai printer les variables respectivement sur matlab et sur python pour que vous voyez. Sous matlab c'est ce que je doit avoir avec python. Or dans mon programme python i n'est pas le même (quand je sors du for à la fin). Qu'est-ce qui ne va pas dans ma traduction ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| %%MATLAB code
....
ii=1;
%% Mainloop
jj = 0;
while ii > 0 && ii<=floor(count/Nbtot)
if mod(ii,Ni/(10*Nbtot)) == 0
jj = jj + 10;
disp([num2str(jj),' %'])
end
nu = Ei; %Energy given to nucleus during the all cascade
xi = []; %positions to calculate the cascade volume
yi = [];
delta = [0 0];
type = 0;
Nbiii = Nbi;
%impact of the molecule at the surface
for ll = [1:Nbtot]
type = floor(3*rand+1); %chose the the ion randomly
while Nbiii(type) == 0 %if the type chosen is not available
type = floor(3*rand+1); %chose the the ion randomly
end
Nbiii(type) = Nbiii(type)-1; %I remove an atom in the number of atoms in the molecule because I will implant it
i = Nbtot*(ii-1) + ll; %rank
ion(i,5) = type; %ion nb i type
E = Ei*Mitot(type)/Mmolecule; %energy of the ion
Mi = Mitot(type);
Zi = Zitot(type);
%disp(Nbtot)=9.0 9.0 9.0 ....
%disp(ii)=1 1 1 1 1.. 2 2 2 2 2.... 3 3 3 3...
%disp(ll)=1.0 2.0 3.0 4.0 5.0 6.0 ...
%disp(i)=1.0 2.0 3.0 4.0 5.0 6.0 ...
%initialisation - Backscattering
levelnb = [1 1 1]; |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| ##PYTHON CODE
....
ii = 1
jj=0
while ii > 0 and ii <= np.floor(count / Nbtot):
if np.remainder(ii,Ni / (10*Nbtot)) == 0:
jj=jj + 10
print(str(jj)+'%')
nu=Ei
xi=np.array([])
yi=np.array([])
delta=np.array(np.hstack((0,0)))
type=0
Nbiii=Nbi
for ll in np.array(np.hstack((np.arange(1, Nbtot+1)))):
type=np.floor(3*np.random.random() + 1)
while Nbiii[int(type)-1] == 0:
type=np.floor(3*np.random.random() + 1)
break
Nbiii[int(type)-1]=Nbiii[int(type)-1] - 1
i=Nbtot*(ii - 1) + ll
ion[int(i)-1,4]=type
E=np.dot(Ei,Mitot[int(type)-1]) / Mmolecule
Mi=Mitot[int(type)-1]
Zi=Zitot[int(type)-1]
#print(Nbtot)=9.0 9.0 9.0 ....
#print(ii)=1 1 1 1 1.. 2 2 2 2 2.... 3 3 3 3...
#print(ll)= 1.0 2.0 3.0 4.0 5.0 6.0 ...
#print(i)=1.0 2.0 3.0 4.0 5.0 6.0 ...
#print(Nbtot)=9.0 9.0 9.0 ....
#print(ii)= 1.0 2.0 3.0 4.0 5.0 6.0 ...
#print(ll)=9.0 9.0 9.0 ....
#print(i)=9.0 18.0 27.0 36.0...
levelnb = np.array(np.hstack((1, 1, 1))) |