필터 지우기
필터 지우기

Loop if for one j

조회 수: 3 (최근 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에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by