For loop does not work properly

9 ビュー (過去 30 日間)
Jonathan Oekland Torstensen
Jonathan Oekland Torstensen 2016 年 8 月 12 日
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...
Jonathan Oekland Torstensen
Jonathan Oekland Torstensen 2016 年 8 月 12 日
I still couldn't figure it out.

サインインしてコメントする。

採用された回答

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
Jonathan Oekland Torstensen 2016 年 8 月 17 日
Solved it! I think, by removing the for loop, and rewriting some code. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by