MATLAB Answers


Seeking help creating a transition probability matrix for a markov chain

Asked by John
on 3 Jan 2013
Latest activity Commented on by Sri Santhosh
on 8 Jan 2015


I was hoping that somebody might be able to help me out in creating a transition probability matrix?

I normally use excel for statistical modelling but this particular problem takes hours to execute using spreadsheets and it is too large and complicated for a spreadsheet .

I have created a variables called 'data' and it contains velocity and acceleration data in 2 columns.

For example

Vel Acc
1   0.28
2   0.28
2   0.00
3   0.28
5   0.56
6   0.28

I was hoping to create a transition probability matrix of the probability of transition from one velocity acceleration pair to another. First of all you would create a frequency matrix counting all the transitions from one velocity acceleration pair to another and convert to a transition probability matrix by dividing by the row total.

Here is a graphical illustration of the matrix.

I would be very grateful if somebody had the time to help me with this. I'm trying to develop my matlab stills but would appreciate if somebody could show me how they would approach the problem.

Kind regards




No products are associated with this question.

1 Answer

Answer by Roger Stafford
on 3 Jan 2013

It would be very similar to the solution I gave in your earlier posting. Let VA be your list of velocities and accelerations.

 [uv,~,nv] = unique(VA(:,1));
 [ua,~,na] = unique(VA(:,2));
 F = accumarray([nv,na],1,[length(nv),length(na)]);
 T = bsxfun(@rdivide,F,sum(F,2));

Again, the rows would correspond to velocity values in uv and the columns to corresponding accelerations in ua.


I set N to the wrong value. It should be the length of u, not the length of n. As it was you should have gotten NaNs on the last 115 rows when normalizing them. Again my apologies. Here is the corrected code. Let me know if there are any further errors.

 [u,~,n] = unique(data,'rows');
 N = length(u);
 F = accumarray([n(1:end-1),n(2:end)],1,[N,N]);
 T = bsxfun(@rdivide,F,sum(F,2));

Yes, Sean's code looks valid to me. He correctly uses 'histc' to choose the next state rather than the more inefficient 'find'. I probably would have generated all the 'rand' values in a vector at one time and then used elements from that vector in 'histc' sequentially in the for-loop but I think it makes little difference.

on 6 Jan 2013

Hello Roger,

Thank you very much for your assistance and for commenting on Sean's code.

All the best

Hello John

Have u succeeded in creating a transition probability matrix for state vectors velocity and acceleration? I am working on the similar task. However, By using the above code provided by Roger, I cannot generate a correct matrix. Could anyone help me?

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!