Loop if for one j

2 次查看(过去 30 天)
Wouter
Wouter 2014-3-12
回答: Chris C 2014-3-13
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 个评论
Wouter
Wouter 2014-3-13
@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 2014-3-13
编辑:Patrik Ek 2014-3-13
while j<some_value && condition?

请先登录,再进行评论。

采纳的回答

Chris C
Chris C 2014-3-13
You need to break out of the j-loop upon that condition and then condition a continue statement within the k-loop.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by