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