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

  • SEARCH
  • Home
  • 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 8428611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:06:16+00:00 2026-06-10T05:06:16+00:00

I am new to Matlab. I was reading this code snippet, but in some

  • 0

I am new to Matlab. I was reading this code snippet, but in some parts (marked with asterisks) I don’t understand what it means, so if anybody could help would be very much appreciated

function [A1nmb] = moran(initsize, popsize) 
% MORAN generates a trajectory of a Moran type process 
%  which gives the number of genes of allelic type A1 in a population 
%  of haploid individuals that can exist in either type A1 or type A2.
%  The population size is popsize and the initial number of type A1 
%  individuals os initsize. 
%  Inputs: initsize - initial number of A1 genes
%          popsize - the total population size (preserved)

if (nargin==0)
  initsize=10;
  popsize=30;
end

A1nmb=zeros(1,popsize);
A1nmb(1)=initsize;

**lambda = inline('(x-1).*(1-(x-1)./N)', 'x', 'N');
mu = inline('(x-1).*(1-(x-1)./N)', 'x', 'N');**

x=initsize;
i=1;
while  (x>1 & x<popsize+1)
  if (lambda(x,popsize)/(lambda(x,popsize)+mu(x,popsize))>rand)
   x=x+1;
   A1nmb(i)=x;
  else
   x=x-1;
   A1nmb(i)=x;
  end;
  i=i+1;
end;
nmbsteps=length(A1nmb);
***rate = lambda(A1nmb(1:nmbsteps-1),popsize) ...
        +mu(A1nmb(1:nmbsteps-1),popsize);***  

**jumptimes=cumsum(-log(rand(1,nmbsteps-1))./rate);**
jumptimes=[0 jumptimes];

stairs(jumptimes,A1nmb);
axis([0 jumptimes(nmbsteps) 0 popsize+1]);
  • 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-10T05:06:18+00:00Added an answer on June 10, 2026 at 5:06 am

    The first line you marked

    lambda = inline('(x-1).*(1-(x-1)./N)', 'x', 'N');
    

    creates something called an inline function. It is equivalent to defining a mathematical function. Example:

    y = inline('x^2')
    

    would allow you to do

    >> y(2)
    4
    

    This immediately explains the second line you marked.

    rate = lambda(A1nmb(1:nmbsteps-1),popsize) ...
        +mu(A1nmb(1:nmbsteps-1),popsize);
    

    will compute the value of the function lambda(x,N) at x = A1nmb(1:nmbsteps-1) and N = popsize.

    I will say immediately here that you should take a look at anonymous functions, a different format used to accomplish the same as inline. Only, anonymous functions are generally better supported, and usually a lot faster than inline functions.

    Then, for the final line,

    jumptimes = cumsum(-log(rand(1,nmbsteps-1))./rate);
    

    is a nested command. rand will create a matrix containing pseudorandom numbers, log is the natural logarithm ("ln"), and cumsum creates a new matrix, where all the elements in the new matrix are the cumulative sum of the elements in the input matrix.

    You will find the commands doc and help very useful. Try typing

    doc cumsum 
    

    or

    help inline
    

    on the Matlab command prompt. Try that again with the commands forming the previous statement.

    As a general word of advice: spend an insane lot of time reading through the documentation. Really, for each new command you encounter, read about it and play with it in a sandbox until you feel you understand it. Matlab only becomes powerful if you know all its commands, and there are a lot to get to know.

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

Sidebar

Related Questions

I'm trying to read some matlab code, but I'm new to it. How do
I'm new at Matlab. You may find this question silly but I really wonder
Sorry if this is stupid question, but I'm new to MATLAB. I have a
I am new to MATLAB so I don't even know if this is possible,
still fairly new to matlab, picked up this data analysis code from someone and
I guess this is something from new version of Matlab. But I can't figure
Am new to matlab. Can someone explain me the following code. this code is
I'm very new to matlab and am more of an ArcGIS user but I'd
Apologies if this is a stupid question, I'm relatively new to Matlab. I've got
I am new to MATLAB, and I can't fathom this from the documentation. function

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.