پنجشنبه, ۱۵ آبان ۱۳۹۹، ۰۹:۲۳ ق.ظ
نزدیکترین نود باشرط کوچکتر یا بزرگتر(find the nearest by condition)
قبلا تابع پیدا کردن کوتاهترین مسیر در دو بعد را تشریح کردم، این بار برای حالت یک بعدی ان شرط بزرگتر بودن یا کوچکتر بودن مقصد را اضافه کردم. در کد زیر فیلد آخر برای تعیین شرط می باشد، اگر 1 باشد یعنی نزدیکترین مقصد را که بزگتر از نود مبداء هست، را انتخاب کن، اگر صفر بود یعنی کتر...
[uniqueDestInd, gridDist1,selctedInd,selctedVal]=getNearstDest1DByCondition(destPos, sourcPos,isplot,isFig,Condition)
کد این تابع:(the matlab code of this function)
i explained this function already but i added a condition, So that, source can get nearest with only if only it be Smaller or larger than the source..
%%%%%%%%%%%%%%%%%%%%%%%%%%-----------software details-----------%%%%%%%%%%%%%%%%%%%%%% %devloped in Iran(ardebil) % %Copyright (C) 2017(1396) by moussa hasanzadeh as the head devloper %% %All rights reserved. % %programed in mathlab 2016 %gmail:mo30no@gmail.com %phone 09147082079 %%caaling script % find nearst dest point(source vector) from source point % sourcPos.x=randi([-100,100],1,100); % destPos.x=randi([-100,100],1,30); % [unDestInd, gridDist,selctedDest,selctedDict]=getNearstDest1D(destPos, sourcPos,1,1,1); % % % sourcPos.y=randi([-100,100],1,100); % destPos.y=randi([-100,100],1,30); % [unDestInd, gridDist,selctedDest,selctedDict]=getNearstDest1D(destPos, sourcPos,1,1,0); % gridDist=gridDist.*(destPos.x' >sourcPos.x); % if Condition==1 % only select destination larger than source else % only select destination lower than source function [uniqueDestInd, gridDist1,selctedInd,selctedVal]=getNearstDest1DByCondition(destPos, sourcPos,isplot,isFig,Condition) gridDist=pdist1D(sourcPos,destPos );%distance between all nodes with grid center points gridDist1=gridDist; if nargin==5 if Condition ==1 % only select destination larger than source gridDist=gridDist.*(sourcPos.x' <destPos.x ); [v,Ind]=max(destPos.x); gridDist(gridDist==0)=max(gridDist(:))+10; [selctedVal,selctedInd]=min(gridDist,[],2);% here we used an simple algorithm to determine each node in network is belong to which cell in grid network selctedInd(Ind)=Ind; selctedVal(Ind)=v ; else% only select destination lower than source gridDist=gridDist.*(sourcPos.x' >destPos.x ); [v,Ind]=min(destPos.x); gridDist(gridDist==0)=max(gridDist(:))+10; [selctedVal,selctedInd]=min(gridDist,[],2);% here we used an simple algorithm to determine each node in network is belong to which cell in grid network selctedInd(Ind)=Ind; selctedVal(Ind)=v ; end else end uniqueDestInd= unique(selctedInd);% 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.x*0+.5,'ro') ;hold on r= rand(1,length(sourcPos.x)) ; plot([sourcPos.x;destPos.x(selctedInd')] ,[sourcPos.x*0+r;sourcPos.x*0+r ],'k-');hold on; if Condition plot([ destPos.x(selctedInd')] ,[ sourcPos.x*0+r ],'>');hold on; else plot([ destPos.x(selctedInd')] ,[ sourcPos.x*0+r ],'<');hold on; end else end end end % gridDist=pdist1D(sourcPos );%distance between all nodes with destnation nodes % gridDist=pdist1D(sourcPos,destPos );%distance between all nodes within a vector function gridDist=pdist1D(s, d ) try xTs=s.x' * ones(1,length(d.x) ); xTd=ones(1,length(s.x) )' * d.x ; gridDist=sqrt((xTs - xTd ).^2 ); catch yTs=s.y' * ones(1,length(d.y) ); yTd=ones(1,length(s.y) )' * d.y ; gridDist=sqrt((yTs - yTd ).^2 ); end end
خروجی این تابع( An output example of this function)
۹۹/۰۸/۱۵