operations on structer under specific conditions ?

조회 수: 2 (최근 30일)
Abdelwahab Fawzy
Abdelwahab Fawzy 2016년 9월 1일
답변: Stephen23 2016년 9월 1일
if there is a structure (S) for 10 nodes. the three fields of the structure are (xd , yd and ID) where,,
S.xd ==> X-dimension , S.yd ==> Y_dimension , S.ID ==> node's order or its ID , S.E ==> Energy
the distance between two nodes i and j equal D(i,j)= sqrt((S(i).xd-S(j).xd)^2+(S(i).yd-S(j).yd)^2).
i want to compare the distance between all nodes, hence if the distance between two nodes is less than specific value (do), we keep the higher energy nodes and neglect the other.
at the end we get the structure of the new higher energy nodes

답변 (3개)

KSSV
KSSV 2016년 9월 1일
편집: KSSV 2016년 9월 1일
You can find the distance between one node to other the other nodes using the following lines:
clc; clear all ;
N = 10 ;
for i = 1:N
s(i).id = i ;
s(i).x = rand(1,1) ;
s(i).y = rand(1,1) ;
end
%%distance between i'th node and other nodes
for i = 1:N
data = repmat([s(i).x s(i).y],[N,1])-[[s.x]' [s.y]'] ;
dist = sqrt(data(:,1).^2+data(:,2).^2);
%%Do what you want
end
  댓글 수: 1
Abdelwahab Fawzy
Abdelwahab Fawzy 2016년 9월 1일
Getting the matrix of the distance between nodes isn't my problem
for i=1:n
for j=1:n
D(i,j)=sqrt((S(i).xd-S(j).xd)^2+(S(i).yd-S(j).yd)^2);
end
end
now i'm interested in finding the nodes which the intersection distance between them < doo
D(D<doo) to compare their energy, hence i can keep the higher energy node and neglect the smaller energy one

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


KSSV
KSSV 2016년 9월 1일
That i snot a big deal...
clc; clear all ;
N = 10 ;
for i = 1:N
s(i).id = i ;
s(i).x = rand(1,1) ;
s(i).y = rand(1,1) ;
end
%%distance between i'th node and other nodes
for i = 1:N
data = repmat([s(i).x s(i).y],[N,1])-[[s.x]' [s.y]'] ;
dist = sqrt(data(:,1).^2+data(:,2).^2);
%%Do what you want
% get the indices whose distance is less then 0.5
idx = find(dist < 0.5) ;
nodes = [[s(idx).x]' [s(idx).y]']
end

Stephen23
Stephen23 2016년 9월 1일
Rather than writing slow and ugly loops, you could simply use bsxfun:
do = 0.9;
S = struct(...
'xd',num2cell(rand(1,10)),...
'yd',num2cell(rand(1,10)),...
'E', num2cell(rand(1,10)));
%
hyp = bsxfun(@hypot,[S.xd].',[S.yd]);
idx = hyp<do
...etc

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by