Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:52:54+00:00 2026-06-10T03:52:54+00:00

It is a basic program to describe the Moran process in the evolutionary game,

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:52:55+00:00Added an answer on June 10, 2026 at 3:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic program that compares two strings : #include <string> #include <iostream>
I have a very basic program in C++ compiled in VS 11 Beta on
i have a really basic java program just one .java/.class file (.java uncompiled .class
I currently have a basic chat program in c++ that uses WinSock2.h with UDP.
If I have the following basic C++ program: #include <iostream> using namespace std; class
I have a Visual Basic program in Excel which opens Word documents and copies
I am new to stackoverflow and the programming world. I have a basic program
I have a very basic Visual Basic Program that I created in Visual Studio
I have a very basic program on GridView with some buttons as in the
I have a very basic program which displays a standard Textview and a extended

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.