Matlab中的递归函数 [英] Recursive Function in matlab

查看:238
本文介绍了Matlab中的递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何在matlab中编写一个递归函数,它基本上是一个马尔可夫链! 我尝试为其编写伪代码,这是 MATLAB 的新手:

How do I write a recursive Function in matlab, it basically being a Markov chain! I tried writing a pseudo code for it and new to MATLAB:

该函数是这样的:

   P= Probability
    x= status(0,1)
    Dij= probability to pick a site
    P(Status of Site(i) being x at next time step)= Summation[P(Status of Site(i) being x   at previous time step)*Dij]

我尝试过代码,有人可以打电话给我吗?

I have tried code, can anyone tel me whether its right:

 function [t,index]= CopyingInfluenceModel
%%Define constants
StatusP1=rand(1,0);
StatusP0=rand(1,0);

% Initializing the variables
t=[];
index=[];
i=[];
%assigining the initial conditions
t(1)=0;
%set index no i to 1(initial condition for i=1)
i=1;
%% If the probability is zero, terminate while loop
while p(i)>=0
%calculate time step for given index no
t(i+1)= t(i);
%calculate the status_probability at given time t=(i+1)
StatusP1(i+1)=StatusP1(i)+sum(StatusP1(i)*Dij);

%index i increases by 1 to calculate next probability
i=i+1;
end
end

推荐答案

首先在matlab中递归"Hello World"阶乘函数:

First the recursive "Hello World" factorial function in matlab:

function result=f(x)
if (x<=0)
    result=1;
else
    result=x*f(x-1);
end

end

可以这样称呼:

>> f(4)
ans =
    24

对Markov链了解不多,我在这里猜测您的函数应该做什么.首先输入代码:

Not knowing much about Markov chains, I am making a guess here at what your function is supposed to do. First the code:

function yourmainscript
    % state 1 -> state 1: 50%
    % state 1 -> state 2: 50%
    % state 2 -> state 1: 25%
    % state 2 -> state 2: 75%
    D=[0.5 0.5; 0.25 0.75];
    p0=[1; 0];

    % Get probability that the system is in state number 1 at time step number 4
    % given the transition matrix D and the initial probabilities p0
    P(1,4,D,p0)
end

function result=P(state, t, D, p0)
if t==0
    result=p0(state);
else
    possible_states=(1:length(D))';
    result=D(state,:)*arrayfun(@(x) P(x, t-1, D, p0), possible_states);
end
end

参数D和p0描述了系统,只是未经修改地传递.只要可以访问它们,使用全局变量或使用函数嵌套也将对其起作用.

The parameters D and p0 describe the system and are just passed through unmodified. Using global variables or playing with function nesting will work for them, too, as long as they are accessible.

state是1到要处理的状态总数之间的整数,t是代表时间步长的整数.

state is an integer between 1 and the total number of states that you deal with, t is an integer that represents the time step.

在t == 0时,我们可以将状态用作p0的索引以获取概率. 对于t> 0,我将总和重写为矩阵乘法:

At t==0 we can use state as an index into p0 to get the probability. For t>0 I have rewritten the sum as a matrix multiplication:

我们需要当前状态给定的Dij行(即D(state,:),并将其与最后一个时间步中所有可能状态的概率向量相乘.

We need the row of Dij (which is D(state,:) given by the current state and multiply it with the vector of the probabilities of all possible states at the last time step.

possible_states=(1:length(D))';

是包含1,2,3,...,最后一个状态的列向量,在下一行中需要. Arrayfun为数组的每个元素(第二个参数)调用一个函数(第一个参数),并将结果填充到向量中.第一个参数是定义以下功能的简写:

is a column vector containing 1,2,3,...,last state and is needed in the next line. Arrayfun calls a function (first argument) for each element of an array (second argument) and stuffs the result into a vector. The first argument is a shorthand for defining the following function:

function result=wrapperfunction(x)
    % Assume t, D and p0 are in scope and the function P is known
    result=P(x, t-1, D, p0);
end

请注意,matlab区分大小写,因此,如果您定义函数"Markov",则matlab现在不再与"markov"有关.

Please note that matlab is case sensitive, so if you define a function "Markov" then matlab still doesn't now about "markov".

抱歉,您在编写此答案时已更新了代码,因此它可能适用于更新的版本,也可能不适用于更新的版本.

Sorry, you have updated your code while I was composing this answer, so it may or may not apply to the updated version.

这篇关于Matlab中的递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆