Why is my if statement making the values of a previous array disappear?

My code basically looks like:
for x = 1:xmax
for y = 1:ymax
for l = 1:xmax
for m = 1:ymax
d(x,y,l,m) = (x-l)^2 + (y-m)^2
if f(x,y,1) ~=f(x,y,5)
%lots of new defined values
else
break
end
end
end
end
end
There is no problem when I remove the if statement, I get the correct array for d. But when the if statement is there the output of d is all zeros. Does anyone know why d is not calculated when I have the conditional in there since it's outside of the if?

답변 (2개)

A Jenkins
A Jenkins 2014년 6월 17일

0 개 추천

break will terminate your for loop, and it won't continue all the way to ymax.
Perhaps you mean you use continue? Or just leave the else case out altogether?

댓글 수: 1

There was one more loop that I didn't put in that is in the code:
for t= 1:tmax
for x = 1:xmax
for y = 1:ymax
for l = 1:xmax
for m = 1:ymax
d(x,y,l,m) = (x-l)^2 + (y-m)^2
if f(x,y,1) ~=f(x,y,5)
%lots of new defined values
else
break
end
end
end
end
end
end
I want the if statement to hold for all x and y and I want the time loop to be stopped if that condition is no longer true.
Is this the correct way to write it?

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2014년 6월 17일

0 개 추천

How would the f array ever change? And, have you tried stepping through the code with the debugger? That's what I do, and everyone else would do. Try this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/

댓글 수: 2

In the portion that I didn't write out because it's too long and not entirely relevant there is a section that says:
f(x,y,t+1) = f(x,y,t)+g(x,y,t)
where g(x,y,t) is also defined. I also start off defining f(x,y,1). So the f array starts off not the same and will eventually get to equilibrium.
And what about my second question?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 6월 17일

댓글:

2014년 6월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by