Clear Filters
Clear Filters

operations on structer under specific conditions ?

2 views (last 30 days)
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

Answers (3)

KSSV
KSSV on 1 Sep 2016
Edited: KSSV on 1 Sep 2016
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 Comment
Abdelwahab Fawzy
Abdelwahab Fawzy on 1 Sep 2016
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

Sign in to comment.


KSSV
KSSV on 1 Sep 2016
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 on 1 Sep 2016
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

Categories

Find more on Dynamic System Models in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!