フィルターのクリア

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 Center および File ExchangeDynamic System Models についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by