user824624
user824624

Reputation: 8080

understanding of Matlab code snippet

It is a basic program to describe the Moran process in the evolutionary game, however with my limited knowledge of Matlab, I have difficulties to quickly understand what the code means. Could someone help me to explain what it means.

If you look at the code, there are a few things I am confused about.

  1. is variable state in the code an array ?
  2. if state is an array, what does it mean to do state(2,:), state(:,2), state(:)
  3. in unique function, what does this statement mean: u(u((1:end-1)')==u((2:end)')) = [];
  4. in mnrnd function, what does this statement mean: r = find(rand < cumsum(p),1);
  5. what is the meaning of frequency = histc(state(:,2), livingTypes)./length(state(:,2));, and in particular, histc?

Here is the function:

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

Upvotes: 0

Views: 819

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78364

1 It certainly looks as if state is a variable and a 2D array at that. If you have the workspace window visible on your desktop (that's the window with the title Workspace not any of the other windows) you should be able to double-click on the variable name and open up the variable editor.

2 state(2,:) means row 2 of the 2D array state; state(:) uses a somewhat confusing (to the beginner) Matlab shorthand to mean all the elements in state considered as a 1D vector, and don't forget that Matlab stores arrays in column-major order. Matlab has the facility to use 1D indexes into greater-than-1-D arrays. You should be able to figure out state(:,2) yourself, if not, play around; one of Matlab's strengths is it's readiness to let you play around.

3 The expression u(u((1:end-1)')==u((2:end)')) = [] is only confusing when you try to take it in all at once. You can work it out with a little care and attention. Start from the inside: (1:end-1) means all the elements of (in this case the vector u ) starting at the first and going on to the last one but one. So, if u has 10 elements this expression selects elements 1:9. The ' is Matlab's transposition operator, it converts a row-vector to a column-vector (and vice-versa). Since it's applied on both sides of the == I'm not sure it serves any purpose here, but experiment with (1:9) and (1:9)' and similar expressions.

Next, the expression u((1:end-1)')==u((2:end)') compares, for equality, the elements u(1:end-1) with the elements u(2:end), in other words it finds those cases where an element of u is the same as its neighbour. Like other boolean operators in Matlab this will return 0 for false and 1 for true -- again experiment with operations such as 1==1 and 1==2. This expression is going to return what Matlab calls a logical index into u, that is it will select those elements of u for which the evaluation is true (or 1).

Finally, with the expression = [] Matlab assigns the empty array to the elements of u selected by the logical indexing. This operation deletes those element.

So, if I've figured this out correctly the statement deletes from u all those elements which are the same as the preceding element.

4 Now it's way past time for you to do some of your own work. find, rand and cumsum are all basic Matlab functions which are well documented. As I suggested above, take some time to figure out what they do in isolation, then start playing around with various combinations of them. You'll catch on much more quickly that way than by reading any more of this drivel.

Upvotes: 1

Related Questions