distance between randomly selected nodes

조회 수: 2 (최근 30일)
rem ng
rem ng 2019년 3월 18일
답변: Walter Roberson 2019년 3월 18일
for i=1:1:n
temp_rand=rand;
if(temp_rand<= some condition)
count_n=count_n+1;
S(i).type='C';
C(cc).xd=S(i).xd;
C(cc).yd=S(i).yd;
plot(S(i).xd,S(i).yd,'*');
cc=cc+1;
In the above code some nodes are selected from randomly distributed nodes which satisfy the temp_rand condition. Please anyone help me how to find out distance between these randomly nodes only.
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2019년 3월 18일
rem - so you want to find the distances between all nodes in the C matrix? Or do you want to find the distance between a different set of nodes?
rem ng
rem ng 2019년 3월 18일
@ Geoff Hayes yes Sir you got the exact question which I am eagerly waiting answer for that. I want to find disttance between all nodes in the C matrix itself.

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 3월 18일
xt = [CC.xd]; yt = [CC.yd];
distances = squareform( pdist([xt(:), yt(:)]) );

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2019년 3월 18일
rem - wouldn't the distances be calculated something like the following
distancesBetweenNodes = zeros(length(C)); % square matrix
for k = 1:length(C)
for j = k+1:length(C)
dist = sqrt((C(k).xd - C(j).xd)^2 + (C(k).yd - C(j).yd)^2); % use your own distance measure here
distancesBetweenNodes(k,j) = dist;
distancesBetweenNodes(j,k) = dist; % matrix is symmetric
end
end
  댓글 수: 1
rem ng
rem ng 2019년 3월 18일
@ Geoff Thanks Sir. I will apply in my code and will update the feedback.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by