Loop if for one j
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm writing a script to simulate the movement of some particles, so I'm working with discrete timesteps i in a for-loop. Inside this loop, I have another for-loop to change position etc. of every particle k. Now, I want to check for every k if there is any of the other particles (for loop over particle j) wich comes too close because than both will annihilate(disappear) which means the new position etc. for k (and j) doesn't need to be computed anymore.
So my question is: how do I check FOR every 1<k<m IF there exist a 1<j<m, j~=k which satisfies certain requirements and IF that's the case move on directly to k+1? ELSE, so if there's no j with the requirements for this k, proceed to the calculation of r(k,i+1)... via a specific algorithm before going to particle k+1.
for i=1:n %timestep
for k=1:m %particle
if isnan(rx(k,i))==1 %I'm checking if it still exists, if not don't do anything and go to the next k
else
for j=1:m %other particles
if isnan(rx(j,i))==0 %check if it still exists
if j~=k
d(k,j)=(rx(k,i)-rx(j,i))^2+(ry(k,i)-ry(j,i))^2;
if d(k,j)<1.5*z0 && s(k)+s(j)==0 %IF THAT'S THE CASE I WANT TO INCREASE K BY ONE IMMEDIATELY
end
... some other stuff about interaction k and j...
end
... some more irrelevant stuff...
end
end
end
... calculate rx(k,i+1) etc ...
end
end
end
Risposta accettata
Chris C
il 13 Mar 2014
You need to break out of the j-loop upon that condition and then condition a continue statement within the k-loop.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!