For loop does not work properly

1 次查看(过去 30 天)
I have the following code:
for
.
.
. Some conditions here
.
.
if (i >= 2)
i
for u=1:size(cent,1)
a(u,:) = sqrt((S(i,1)-cent(u,1))^2 + (S(i,2)-cent(u,2))^2 + (S(i,3)-cent(u,3))^2)
stor = size(cent,1)
if a <= 2*r/1000;
continue;
elseif stor >= e
break;
else
con = size(cent,1) + 1
cent(con,:) = S(i,:)
end
end
end
end
cent
So what I want the for loop to add S(i,:) as a matrix row if it satisfies the if condition, and if the numbers of rows does not exceed e. If it does not match the a condition, I want it to go back to the first for loop, and then recalculate S.
The output I get, shows that it works almost fine, but duplicates some rows. Why?
Output:
i =
2
a =
0.4423
stor =
1
con =
2
cent =
0.7738 0.8484 0.9085
0.8241 0.4105 0.8714
i =
3
a =
0.6804
stor =
2
con =
3
cent =
0.7738 0.8484 0.9085
0.8241 0.4105 0.8714
0.1843 0.8812 0.5704
a =
0.6804
0.8493
stor =
3
con =
4
cent =
0.7738 0.8484 0.9085
0.8241 0.4105 0.8714
0.1843 0.8812 0.5704
0.1843 0.8812 0.5704
i =
4
a =
0.7034
stor =
4
con =
5
cent =
0.7738 0.8484 0.9085
0.8241 0.4105 0.8714
0.1843 0.8812 0.5704
0.1843 0.8812 0.5704
0.6145 0.3154 0.4782
  2 个评论
dpb
dpb 2016-8-12
This is a perfect spot to use the debugger and step through the code and see where your logic error is...

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2016-8-12
if a <= 2*r/1000;
means the same thing as
if all(all(a <= 2*r/1000))
which is to say it is only considered true if all of the values in a satisfy the comparison. If even one of them does not satisfy the comparison, then control will pass on to the elseif branch. If that elseif branch is also not satisfied then the else to it could end up being executed multiple times for the same i value with different u values.
  3 个评论
Walter Roberson
Walter Roberson 2016-8-12
You have
a(u,:) = sqrt((S(i,1)-cent(u,1))^2 + (S(i,2)-cent(u,2))^2 + (S(i,3)-cent(u,3))^2)
so you are adding rows to a (or perhaps filling them in; we do not see how you initialized.) That new row and all previous rows are tested simultaneously with your if. If there is a failure to match anywhere along the line, you check to see whether size(cent,1) >= some value e that we do not know anything about. We have no reason to expect that the cent will already have grown as large as the target e (whatever value that is) so it is reasonable to suspect that the else might be reached multiple times before finally cent grows large enough that the break is triggered.
Before, by the way:
for u=1:size(cent,1)
that will not take into account any growth in cent inside the loop. The start and end values and increment values for a for loop are calculated once and no changes to any variables in the expression will affect the number of times the loop is done. But meanwhile you have confused the readers by growing cent inside the loop. Did you know the expression would not be re-evaluated? Are you sure that you want u to start from 1 even though you are growing cent as you go?
Jonathan Oekland Torstensen
Solved it! I think, by removing the for loop, and rewriting some code. Thanks!

请先登录,再进行评论。

更多回答(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