这是一个描述进化博弈中Moran过程的基本程序,但是由于我对Matlab的了解有限,我很难快速理解代码的含义。有人可以帮我解释一下这是什么意思。
如果您查看代码,我会感到困惑。
- 代码中的变量
state
是数组吗? - if
state
是一个数组,这意味着什么state(2,:), state(:,2), state(:)
- 在
unique
功能上,这句话是什么意思:u(u((1:end-1)')==u((2:end)')) = [];
- 在
mnrnd
功能上,这句话是什么意思:r = find(rand < cumsum(p),1);
- 是什么意思
frequency = histc(state(:,2), livingTypes)./length(state(:,2));
,特别是,histc
?
这是功能:
function state = moranprocess(initialState, nSteps, tau)
%# assume 1 step if not specified
if(nargin < 2)
nSteps = 1;
end
%# if it isn't specified, assume frequency indepdence.
if(nargin < 3)
tau = 0;
end
%# initialize with starting state
state = initialState;
%# perform the moran process
for t = 1:nSteps
state(selection(state, 0), :) = state(selection(state, tau), :);
end
end
%# frequency dependent selection with parameter tau determining the
%# strength of selection
function i = selection(state, tau)
%# find all of the living types
livingTypes = unique(state(:,2))';
%# create a multinomial of living type frequencies.
frequency = histc(state(:,2), livingTypes)./length(state(:,2));
%#frequency = makemultinomial(state(:,2));
fitness = (frequency.^tau)./sum(frequency.^tau);
%# selection is proportional to fitnesss
selected_type = livingTypes(mnrnd(1, (frequency.*fitness) ./ sum(frequency.*fitness)));
%# choose randomly among those of the selected type
thoseOfSelectedType = find(state(:,2) == selected_type);
i = thoseOfSelectedType(ceil(length(thoseOfSelectedType)*rand));
end
%# fast unique
function u = unique(x)
u = sort(x(:));
u(u((1:end-1)')==u((2:end)')) = [];
end
%# fast mnrnd
function r = mnrnd(n,p)
r = find(rand < cumsum(p),1);
end