-1

我试图在 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]

这就是我所做的!但我的索引总是超过矩阵尺寸!我需要这方面的帮助。

clear all;
clc;

%function [t,i]= CopyingInfluenceModel
%%Define constants
%% generate some random weights vectori.e. the transition matrix=C
 % C=[0 (1,2) 0 (1,4) 0 0 0;
 %    (2,1) 0 (2,3) 0 0 0 0;
 %    0 (3,2) 0 (3,4) 0 0 0;
 %    (1,4) 0 (4,3) 0 (4,5) 0 0;
 %    0 0 0 (5,4) 0 (5,6) (5,7);
 %    0 0 0 0 (6,5) 0 (6,7);
 %    0 0 0 0 (7,5) (7,6) 0];
 %copying probabilities=branch weights
 onetwo=0.47;
 twothree=0.47;
 threefour=0.47;
 onefour=0.47;
 fourfive=0.023;
 fivesix=0.47;
 fiveseven=0.47;
 sixseven=0.47;
 selfweight1=0.06;
 selfweight2=0.037;
 % SourceNodes - a list of Nodes that are forced to be kept in one side of the cut.
 % WeightedGraph - symetric matrix of edge weights. Wi,j is the edge
 % connecting Nodes i,j  use Wi,j=0 or Wi,j == inf to indicate unconnected Nodes                  

 WeightedGraph=[0 onetwo 0 onefour 0 0 0;
 onetwo 0 twothree 0 0 0 0;
 0 twothree 0 threefour 0 0 0;
 onefour 0 threefour 0 fourfive 0 0;
 0 0 0 fourfive 0 fivesix fiveseven;
 0 0 0 0 fivesix 0 sixseven;
 0  0 0 0 fiveseven sixseven 0];
 Dij=sparse(WeightedGraph);


 % Initializing the variables
 t=[];
 i=[];
 %assigining the initial conditions
 t(1)=0;
 p(1)= 0.003; %% initial probability of status
 %set index no i to 1(initial condition for i=1)
 i=1;
 %repeating calculating new probabilities

 %% If the probability is zero, terminate while loop
 while p(i)>=0
    %calculate at the next time step for given index no
    t(i+1)= t(i);
    %calculate the status_probability at given time t=(i+1)
    [p(i+1)]=[p(i)]+sum([p(i)]*[Dij(i)]);
    [NextStatus(i)]= [p(i+1)]

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

堆栈跟踪是:

%%??? Index exceeds matrix dimensions. 
%%Error in ==> CopyingInfluenceModel at 54 
%%[p(i+1)]=[p(i)]+sum([p(i)]*[Dij(i)]);
4

2 回答 2

1

问题Dij不是pDij具有固定长度,因此当i超过该长度时程序会抛出错误。

添加:

我在代码中看不到你的逻辑,但我有一种强烈的感觉,你在计算错误。Dij是一个 7 x 7 矩阵,但您可以通过调用将其视为向量Dij(i)。如果您试图将某物乘以行或列,则需要Dij(i,:)orDij(:, i)符号。

于 2012-11-16T19:19:00.290 回答
0

您发布的逻辑不起作用,本质上, p(i+i) 尚未定义。有几种方法可以做到这一点,具体取决于您是否要保留 p 。我将发布一个保留 p 的方法,但可以做一些工作来提高代码效率。

p=[p;p(i)+sum(p(i)*Dij(i))];
NextStatus(i)= p(i+1)
于 2012-11-16T17:27:56.950 回答