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

# Transition probability matrix for markov chain

Asked by John on 2 Sep 2011
Latest activity Answered by Hanane on 14 Mar 2014 at 22:22

Hi there

I have time, speed and acceleration data for a car in three columns. I'm trying to generate a 2 dimensional transition probability matrix of velocity and acceleration.

The concept is given a particular speed and acceleration I would like to know the next most likely (probable) speed and acceleration.

I have some code below, but cannot fully understand it. Will it generate a 2D transition matrix or a 4D transition matrix?

Thank you

```   %% First bin data into categories
speedBinN = 5;
aceelBinN = 5;
speed = binit( data(:,2), linspace(min(data(:,2)),max(data(:,2)),speedBinN) ); % bin them into categories
accel = binit( data(:,3), linspace(min(data(:,3)),max(data(:,3)),aceelBinN) );```
```%% count up transitions
transCountMat = zeros(speedBinN,aceelBinN,speedBinN,aceelBinN);
for ii = 1:size(data,1)-1
transCountMat( speed(ii),accel(ii),speed(ii+1),accel(ii+1) ) = transCountMat( speed(ii),accel(ii),speed(ii+1),accel(ii+1) ) + 1;
end
```
```%% calculate probabilities
sumOverPossibleDestinations = sum( sum(transCountMat, 4), 3);
transMat = bsxfun( @rdivide, transCountMat, sumOverPossibleDestinations );
```
```%% User Interactive stuff
IM = imagesc(squeeze(transMat(1,1,:,:)));
colorbar
set(IM,'ButtonDownFcn',@bdFcn)
set(gca,'ydir','normal')
ylabel speed
xlabel accel
hold on
p = plot(1,1,'w');
updateIndicator(1,1)
```

## Products

No products are associated with this question.

Answer by Walter Roberson on 2 Sep 2011

Looks to me like it will generate a 2D output for transMat. The count matrix is 4 dimensional, but it is summed twice, which reduces that to 2 dimensions.

Looks to me like binit() is just the second output of histc(). With the linspace nature of the bins, that operation could probably be made more efficient than even histc(). Also the transcount loop could probably be replaced with a single accumarray call.

Answer by John on 3 Sep 2011

Hi Walter,

Thank you for your reply and for explaining this to me. I'm happy that it generates a 2D output.

The code prints out the values of each element in the matrix. For example

```val(:,:,1,3) =
```
```       NaN    0.2000    0.7692    0.0043         0
0         0    0.1294         0       NaN
0         0         0         0       NaN
NaN       NaN         0         0       NaN
NaN       NaN       NaN         0       NaN```

How can I make it just print out the actual matrix? I understand that this is probably a simple question but I don't know to do this.

Thank you

Walter Roberson on 3 Sep 2011

There is no "val" in the code you show, so I am unsure what you are asking about?

When MATLAB displays a multidimensional matrix (4 dimensions in this case), it displays a "page" at a time, where a "page" is the first 2 dimensions. What format would you like the 4 dimensional matrix printed out in?

Walter Roberson on 3 Sep 2011

Hmmm, looking again, it appears that transMat will be 4 dimensional, not the 2 dimensional that I thought.

Answer by John on 5 Sep 2011

Hi Walter,

Would you know how to change the code to produce a 2D transition probability matrix of velocity and acceleration?

I've uploaded a sample data set to sendspace.com http://www.sendspace.com/file/oqx54r

And here is the full code

```function transMat = mwExample
```
```load('Sample data set mathworks.mat')
```
```%% First bin data into categories
speedBinN = 5;
aceelBinN = 5;
speed = binit( data(:,2), linspace(min(data(:,2)),max(data(:,2)),speedBinN) ); % bin them into categories
accel = binit( data(:,3), linspace(min(data(:,3)),max(data(:,3)),aceelBinN) );
```
```%% count up transitions
transCountMat = zeros(speedBinN,aceelBinN,speedBinN,aceelBinN);
for ii = 1:size(data,1)-1
transCountMat( speed(ii),accel(ii),speed(ii+1),accel(ii+1) ) = transCountMat( speed(ii),accel(ii),speed(ii+1),accel(ii+1) ) + 1;
end
```
```%% calculate probabilities
sumOverPossibleDestinations = sum( sum(transCountMat, 4), 3);
transMat = bsxfun( @rdivide, transCountMat, sumOverPossibleDestinations );
```
```%% User Interactive stuff
IM = imagesc(squeeze(transMat(1,1,:,:)));
colorbar
set(IM,'ButtonDownFcn',@bdFcn)
set(gca,'ydir','normal')
ylabel speed
xlabel accel
hold on
p = plot(1,1,'w');
updateIndicator(1,1)
```