ga_rand.m

来自「其中提到遺傳學的程式碼與應用提供給次淚相向的研究者參考下載」· M 代码 · 共 37 行

M
37
字号
function randValue=GA_rand(value,DIR);
%% randValue=GA_rand(value,DIR);
%% value must be a scalar.
%% If DIR== 1, return a numeric greater than value
%% If DIR==-1, return a numeric smaller than value
%% If DIR== 0, return a randon numeric
%% For example:
%value=3;DIR=-1;
%randValue=GA_rand(value,DIR)

% PenChen Chou. 2004-10-29

if nargin==1, DIR=0; end
[m,n]=size(value); 
if m*n~=1, error('==>ERROR! value must must be a scalar.'); end
if DIR==1 | DIR==0 | DIR==-1, 
    ;
else
    error ('DIR must be -1/0/1'); 
end
rand('state',sum(100*clock));
if value>0
    LB=0.5*value; UB=2*value;
elseif value<0
    LB=2*value; UB=0.5*value;
else
    LB=-0.005*ones(size(value)); UB=0.005*ones(size(value));
end
OK=0;
while ~OK
   randValue=(UB-LB)*rand+LB;
   if DIR==0, OK=1; end
   if randValue>1.50*value & DIR== 1, OK=1; end
   if randValue<0.67*value & DIR==-1, OK=1; end
   if value==0, OK=1; end
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?