set.m
来自「一些用matlab编写的经典遗传算法算例 挺有用的」· M 代码 · 共 47 行
M
47 行
function c = set(chr,varargin)
% SET - Acces to the fields in the chromosome class
% Any number of valid properties can be set with one function
% call. The syntax for calling the function is:
% set(chromosome,'property',value,'property',value,etc..). The
% properties are:
%
% 'name' - the name of the variable
% 'fitness' - the value of the variable
% 'cdvs' - number of coding bits
% 'ddvs' - a vector containing the binary "dna"
%
% to list the valid properties for a certain object use set(chromosome)
tst=nargin;
if tst==1
fprintf('Valid properties:\nname \nfitness \ncdvs\nddvs\nid\n')
else
if ~or(or(tst==3,tst==5),tst==7)
error('wrong number of arguments')
else
for k=1:2:tst-1
switch varargin{k}
case 'name'
chr.name=varargin{k+1};
case 'fitness'
chr.fitness=varargin{k+1};
case 'cdvs'
chr.cdvs=varargin{k+1};
case 'ddvs'
chr.ddvs=varargin{k+1};
case 'id'
chr.id=varargin{k+1};
otherwise
error('Unknown method')
end
end
c=chr;
end
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?