دوشنبه, ۴ آذر ۱۳۹۸، ۰۴:۳۴ ب.ظ
تابع کوتاهترین مسیر، getNearstDest2D
این تابع دارای چهار ورودی و چهار خروجی هست، ورودی اول ایدی نودهای مقصد(مثلا سرخوشه) ورودی دوم ایدی نودهای مبدا(مثلا نودهای عادی که قرار از بین سرخوشه های یکی را انتخاب کنند، ورودی سوم فلق isplot( ایا مسیر بین مبدا و مقصدها رسم شود) ، ورودی چهارمisfigur ( ایا فرم جدیدی باز شود یا نه؟) خروجی اول unDestInd( مجموعه نودهای مقصد منتخب یونیک)، خروجی دوم gridDist( ماتریس فاصله بین نودهای مبدا و مقصد، خروجی سوم selctedDest( بردار ایدی مجموعه نودهای مقصد منتخب غیر یونیک ) خروجی چهارم selctedDict( کمترین فواصل انتخاب شده بین نودهای مبداء و مقصد).
%%caaling script % find nearst dest point(source vector) from source point % sourcPos.x=randi([-100,100],1,100); % sourcPos.y=randi([-100,100],1,100); % % destPos.x=randi([-100,100],1,30); % destPos.y=randi([-100,100],1,30); % [unDestInd, gridDist,selctedDest,selctedDict]=getNearstDest2D(destPos, sourcPos,1,1); %% function [unDestInd, gridDist,selctedDest,selctedDict]=getNearstDest2D(destPos, sourcPos,isplot,isFig) gridDist=pdist2D(sourcPos,destPos );%distance between all nodes with grid center points [selctedDict,selctedDest]=min(gridDist,[],2);% here we used an simple algorithm to determine each node in network is belong to which cell in grid network unDestInd= unique(selctedDest);% if we unique the number of centers that are selected by normal nodes, based on minimum distance, in reality we get the number coverage area if nargin >=3 if isplot==1 if isFig figure; end plot( sourcPos.x, sourcPos.y,'ro') ;hold on plot( destPos.x ,destPos.y,'gd') ;hold on plot([sourcPos.x;destPos.x(selctedDest')] ,[sourcPos.y;destPos.y(selctedDest')],'k-');hold on; else end end end % gridDist=pdist2D(sourcPos );%distance between all nodes with destnation nodes % gridDist=pdist2D(sourcPos,destPos );%distance between all nodes within a vector function gridDist=pdist2D(s, d ) if nargin==1 xTs=s.x' * ones(1,length(s.x) ); yTs=s.y' * ones(1,length(s.y) ); xTd=ones(1,length(s.x) )' * s.x ; yTd=ones(1,length(s.y) )' * s.x ; gridDist=sqrt((xTs - xTd ).^2 +(yTs - yTd ).^2 ); else xTs=s.x' * ones(1,length(d.x) ); yTs=s.y' * ones(1,length(d.y) ); xTd=ones(1,length(s.x) )' * d.x ; yTd=ones(1,length(s.y) )' * d.y ; gridDist=sqrt((xTs - xTd ).^2 +(yTs - yTd ).^2 ); end end
نمونه خروجی این تابع
۹۸/۰۹/۰۴