how to extract the value from voronoi diagram in matlab?
6 次查看(过去 30 天)
显示 更早的评论
I need to know how to extract the value from the "voronoi" diagram in matlab. Please reply to me as soon as possible.
I'm plotting this graph by using this command "voronoi". And now I want to extract the value of each "node" in each "cluster".
2 个评论
Geoff Hayes
2014-9-3
Arul - are you doing more than just calling voronoi to get the above plot? What are the lines of code that you are calling to produce the above? The cluster heads are presumably the coordinates for the points passed into the voronoi function, but where do the other ones come from?
采纳的回答
Geoff Hayes
2014-9-2
Arul - if you want to get the vertices for each Voronoi cell, then use voronoin. For example, from this link, [V,C] = voronoin(X) returns Voronoi vertices V and the Voronoi cells C of the Voronoi diagram of X.
14 个评论
Matlab111
2014-9-3
No actually my problem is this http://www.mathworks.in/matlabcentral/answers/153084-how-to-extract-the-neighborhood-node-value-from-this-graph just look at this than try to tell the solution plz.
Geoff Hayes
2014-9-3
The above link is invalid. Can you post the code that you have used to produce the above plot?
Matlab111
2014-9-3
编辑:Matlab111
2014-9-3
close all;
clear all;
%Field Dimensions - x and y maximum (in meters)
xm=100;
ym=100;
sink.x=0.5*xm;
sink.y=0.5*ym;
n=100;
p=0.1;
Eo=0.1;
ETX=50*0.000000001;
ERX=50*0.000000001;
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
EDA=5*0.000000001;
m=0.1;
x=0.2;
a=1;
b=0.5;
rmax=5;
Ave_CH=0;
sum=0;
count_ch=0;
Throughput=0;
Packet=40;
do=sqrt(Efs/Emp);
figure(1);
for i=1:1:n
S(i).xd=rand(1,1)*xm;
XR(i)=S(i).xd
S(i).yd=rand(1,1)*ym;
YR(i)=S(i).yd
S(i).G=0;
S(i).E=0;
S(i).type='N'
keep(i)=i;
temp_rnd0=i;
if (temp_rnd0>=(x+m)*n+1)
S(i).E=Eo;
S(i).ENERGY=0;
plot(S(i).xd,S(i).yd,'o');
hold on;
end
end
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
figure(1);
countCHs=0;
rcountCHs=0;
cluster=1;
countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;
flag_first_Hdead=0;
flag_last_dead=0;
c=1;
for r=0:1:rmax
for i=1:1:n
if(S(i).E>0)
holder(i)=S(i).E;
id(i)=keep(i);
node= struct('energy', holder, 'id',id);
[energy,index] = sort([node.energy],'descend');
end
end
total=0;
for k=1:length(node.energy)
energy_level=sort(node.energy, 'descend');
total=total + node.energy(k);
end
average=total/length(node.energy);
TEnergy(r+1)=total;
AveEnergy(r+1)=average;
r
%Election Probability for Normal Nodes
pnrm=( p/ (1+a*m+b*x) );
if(mod(r, round(1/pnrm) )==0)
for i=1:1:n
S(i).G=0;
S(i).cl=0;
end
end
end
Hdead=0;
dead=0;
packets_TO_BS=0;
packets_TO_CH=0;
PACKETS_TO_CH(r+1)=0;
PACKETS_TO_BS(r+1)=0;
figure(1);
for i=1:1:n
if (S(i).E<=(Eo/2)) && (S(i).E>0)
plot(S(i).xd,S(i).yd,'yellow .');
Hdead=Hdead+1;
if(S(i).ENERGY==1)
Hdead_a=Hdead_a+1;
end
hold on;
end
if (S(i).E<=Eo)||(S(i).E>Eo)
if(S(i).ENERGY==0)
RnEnergy(r+1)=S(i).E;
end
end
if (S(i).E<=0)
plot(S(i).xd,S(i).yd,'red .');
dead=dead+1;
if(S(i).ENERGY==1)
dead_a=dead_a+1;
end
hold on;
end
if S(i).E>0
S(i).type='N'
if (S(i).ENERGY==0)
plot(S(i).xd,S(i).yd,'o');
end
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'x');
HSTATISTICS(r+1).DEAD=Hdead;
HDEAD(r+1)=Hdead;
if (Hdead==1)
if(flag_first_Hdead==0)
first_Hdead=r;
flag_first_Hdead=1;
end
end
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
if (dead==1)
if(flag_first_dead==0)
first_dead=r;
flag_first_dead=1;
end
end
alive=0;
for i=1:1:n
if (S(i).E>0)
alive=alive+1;
if(S(i).ENERGY==1)
alive_a=alive_a+1;
end
hold on;
end
if (S(i).E>0)
nodes_status=1;
end
if (S(i).E<0)
nodes_status=0;
end
ASTATISTICS(r+1).Live=alive;
Live(r+1)=alive;
end
countCHs=0;
cluster=1;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0)
if( ( S(i).ENERGY==0 && ( temp_rand <= ( pnrm / ( 1 - pnrm * mod(r,round(1/pnrm)) )) ) ) )
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
S(i).type='C'
S(i).G=100;
C(cluster).xd=S(i).xd
C(cluster).yd=S(i).yd
plot(S(i).xd,S(i).yd,'red+');
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=distance
C(cluster).id=i
X(cluster)=S(i).xd
Y(cluster)=S(i).yd
cluster=cluster+1;
distance;
if (distance>do)
S(i).E=S(i).E-((ETX+EDA)*(Packet)+Emp*Packet*( distance*distance*distance*distance ));
Energy_CH_N=(ETX+EDA)*(Packet) + Emp*Packet*( distance*distance*distance*distance );
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(Packet) + Efs*Packet*( distance * distance ));
Energy_CH_N=(ETX+EDA)*(Packet) + Emp*Packet*( distance*distance);
end
end
end
end
end
warning('OFF');
[vx,vy]=voronoi(X,Y);
voronoi(X,Y);
axis([0 xm 0 ym]);
Geoff Hayes
2014-9-3
Okay - if I run the above code, there always seems to be 101 nodes and a varying number of cells/clusters. If you want to find which nodes belong to which clusters, then just calculate the distance from each node to each cluster "centre" and that distance which is the shortest is the cluster that the node belongs to. Something like
% pre-allocate memory to the node~cluster array
clusterData = cell(size(X,2),1);
% iterate over each node
for k=1:size(S,2)
x = S(k).xd;
y = S(k).yd;
minDist = Inf;
at = 0;
% iterate over each cluster
for u=1:size(X,2)
dist = sqrt((x-X(u))^2+(y-Y(u))^2);
if dist<minDist
% distance is shorter, so the node is closer to this cluster
minDist = dist;
at = u;
end
end
% add the node to the array of clusters
if at>0
clusterData{at} = [clusterData{at} ; S(k)];
end
end
The above code will group each node into the cluster that it belongs. So if there are 12 clusters/cells, then the size of clusterData is 12 with each element having zero or more nodes.
Geoff Hayes
2014-9-3
Arul - you will need to describe (to me) what the cluster head position is. What in your code represents the cluster head? Are the cluster heads different from the input to the call to voronoi at
[vx,vy]=voronoi(X,Y);
Matlab111
2014-9-4
1.'X' and 'Y' represents the cluster head positions.
2. X(cluster)=S(i).xd
Y(cluster)=S(i).yd
3."Are the cluster heads different from the input to the call to voronoi at [vx,vy]=voronoi(X,Y);" -No,the Cluster head is same.
Geoff Hayes
2014-9-4
So, change the above code to the following to get the positions of each cluster head and the positions of each node within that cluster
clusterData = cell(size(X,2),1);
for k=1:size(clusterData)
clusterData{k}.clusterHead = [X(k) Y(k)];
clusterData{k}.clusterNodes = [];
end
for k=1:size(S,2)
x = S(k).xd;
y = S(k).yd;
minDist = Inf;
at = 0;
for u=1:size(X,2)
dist = sqrt((x-X(u))^2+(y-Y(u))^2);
if dist<minDist
minDist = dist;
at = u;
end
end
if at>0
clusterData{at}.clusterNodes = ...
[clusterData{at}.clusterNodes ; [x y] ];
end
end
Now clusterData will be a cell array of structs where each struct has two fields: the clusterHead which is the (x,y) coordinate of the cluster head, and an array of cluster node positions. For example,
clusterData{1}
ans =
clusterHead: [92.0332039836564 5.26769976807926]
clusterNodes: [5x2 double]
and
clusterData{1}.clusterNodes
ans =
72.122749858174 10.6761861607241
68.1971904149063 4.24311375007417
82.5313795402046 8.3469814858914
80.336439160244 6.04711791698936
92.0332039836564 5.26769976807926
Matlab111
2014-9-4
编辑:Matlab111
2014-9-4
ya sir i'm getting, now i want to do multihop communication. which means, like this
in this diagram that read color line represent's finding the nearer cluster head by communicating through the neighborhood node. so now i want to find nearer cluster head through the nodes.Display which cluster head is nearer and display other cluster head and display through which node it's finding.
Geoff Hayes
2014-9-4
Arul - please create a new question as what you are requesting (in your above comment) has nothing to do with the original question that began this thread.
Matlab111
2014-10-14
编辑:Matlab111
2014-10-14
http://www.mathworks.in/matlabcentral/answers/153186#comment_235285 sir from your code i can get an cluster head and cluster head member. But i want to get their residual energy also ('energy'(S.E in my code)).For every node i want.(i need both cluster head and cluster member energy).
Mark Neoglobal
2015-9-20
hello. i am using this code and need to display the number of packets received and sent by each cluster head. how is it possible. any help?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Voronoi Diagram 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)