I have to generate transition probability matrix from sample data.
8 views (last 30 days)
Show older comments
I have the following code of generating transition probability matrix.
data=randi(12,[5,3]);
%%First bin data into categories
speedBinN = 5;
aceelBinN = 5;
speed = histc( data(:,2), linspace(min(data(:,2)),max(data(:,2)),speedBinN) ); % bin them into categories
accel = histc( 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)
but it generates the following error:
Attempted to access transCountMat(2,1,0,1); index must be a positive integer or logical.
Error in Untitled2 (line 12)
transCountMat( speed(ii),accel(ii),speed(ii+1),accel(ii+1) ) = transCountMat(
speed(ii),accel(ii),speed(ii+1),accel(ii+1) ) + 1;
Plz resolve it.
0 Comments
Answers (1)
Shaunak
on 20 Feb 2025
Hi Sami,
It is my understanding that you're trying to generate a transition probability matrix in MATLAB, but you're encountering an error related to indexing:
"Attempted to access transCountMat(2,1,0,1); index must be a positive integer or logical."
This issue arises because one of your indices is zero, which is not allowed in MATLAB.
You can use the ‘discretize’ function instead of ‘histc’ to ensure that your indices are valid positive integers. This will help avoid the zero-indexing issue.
Here’s an alternative way for your reference:
data = randi(12, [5, 3]);
% First bin data into categories
speedBinN = 5;
accelBinN = 5;
% Define bin edges
speedEdges = linspace(min(data(:,2)), max(data(:,2)), speedBinN+1);
accelEdges = linspace(min(data(:,3)), max(data(:,3)), accelBinN+1);
% Bin the data
speed = discretize(data(:,2), speedEdges);
accel = discretize(data(:,3), accelEdges);
% Count up transitions
transCountMat = zeros(speedBinN, accelBinN, speedBinN, accelBinN);
for ii = 1:size(data, 1) - 1
if speed(ii) > 0 && accel(ii) > 0 && speed(ii+1) > 0 && accel(ii+1) > 0
transCountMat(speed(ii), accel(ii), speed(ii+1), accel(ii+1)) = ...
transCountMat(speed(ii), accel(ii), speed(ii+1), accel(ii+1)) + 1;
end
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) % Assuming the function updateIndicator is defined
You can find more information on the ‘discretize’ function in the following MATLAB documentation: https://www.mathworks.com/help/matlab/ref/double.discretize.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!