Loop if for one j

2 views (last 30 days)
Wouter
Wouter on 12 Mar 2014
Answered: Chris C on 13 Mar 2014
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
  3 Comments
Wouter
Wouter on 13 Mar 2014
@dpb if I try continue, it just moves to the next j but I want to the next k. And if I try break, it moves to the rest of the code behind the j-loop but still for that k.
Patrik Ek
Patrik Ek on 13 Mar 2014
Edited: Patrik Ek on 13 Mar 2014
while j<some_value && condition?

Sign in to comment.

Accepted Answer

Chris C
Chris C on 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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!