how to implement loops in a sturcture

1 次查看(过去 30 天)
Anum
Anum 2013-3-27
i have four nodes, i want to distances between each pair of node. there are four different structures and each one of them has a leader node, as ldr1, ldr2, ldr3 and ldr4. and each leader node per structure is the 13th node. My code is:
ldr1.x = 32;
ldr1.y = 42;
S1(13).x = ldr1.x;
S1(13).y = ldr1.y;
ldr2.x = 10;
ldr2.y = 15;
S2(13).x = ldr2.x;
S2(13).y = ldr2.y;
ldr3.x = 45;
ldr3.y = 50;
S3(13).x = ldr3.x;
S3(13).y = ldr3.y;
ldr4.x = 22;
ldr4.y = 19;
S4(13).x = ldr4.x;
S4(13).y = ldr4.y;
now i want to calculate distance of leader1 with leader2,3,4. similarly distance of leader2 with leader 1,3,4 and so on.
i have used this line:
for k = 1 : 4
dis_ldr1_ldr2 = sqrt((S(k)((13)).x - S(k+1)((13)).x)^2 + (ldr(k).y - ldr(k+1).y)^2);
end
but this line is giving error. Kindly help

回答(1 个)

Doug Hull
Doug Hull 2013-3-27
There is a lot of indexing and confusion going on here.
I recommend breaking down the problem into smaller, testable pieces. Here is what I mean, but it is untested.
pointAStructure = S(k)
PointAxValue = pointAStructure.x
PointAyValue = pointAStructure.y
pointBStructure = S(k+1)
PointBxValue = pointBStructure.x
PointByValue = pointBStructure.y
deltaX = PointAxValue - PointBxValue;
deltaY = PointAyValue - PointByValue;
distance = sqrt(deltaX^2 + deltaY^2);
---------
Your naming convention for the structure S1,S2,S3,S4 does not work well:
  1 个评论
Anum
Anum 2013-3-27
there will be 12 distances need to be determined. i want to summarize it whitin a loop

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by