Frage

I was trying to get this low order recursive function in matlab. i want to calculate the probability of status of a site at next time step, given that I have the initial probability of that being a status.

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]

and this is what I have done! but my index always exceeds matrix dimensions! I need help with this.

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

Stack Trace is:

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

Lösung

The problem is Dij not p. Dij has a fixed length so when i exceeds that the program throws an error.

Added:

I can't really see your logic in the code, but I have a strong feeling that you are calculating something wrong. Dij is a 7 x 7 matrix but you treat it as a vector by calling Dij(i). If you are trying to multiply something by a row or column, you need the Dij(i,:) or Dij(:, i) notation.

Andere Tipps

The logic as you posted it doesn't work, essentially, p(i+i) isn't defined yet. There are a few ways to do it, depending on if you want to keep p or not. I'll post a method that keeps p around, but some work could be done to make the code more efficient.

p=[p;p(i)+sum(p(i)*Dij(i))];
NextStatus(i)= p(i+1)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top