Effacer les filtres
Effacer les filtres

operations on structer under specific conditions ?

2 vues (au cours des 30 derniers jours)
Abdelwahab Fawzy
Abdelwahab Fawzy le 1 Sep 2016
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

Réponses (3)

KSSV
KSSV le 1 Sep 2016
Modifié(e) : KSSV le 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 commentaire
Abdelwahab Fawzy
Abdelwahab Fawzy le 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

Connectez-vous pour commenter.


KSSV
KSSV le 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 le 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

Catégories

En savoir plus sur Dynamic System Models dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by