operations on structer under specific conditions ?
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (3)
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
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
0 Comments
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
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!