LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
dtmc_makestochastic.m
1%{ @file dtmc_makestochastic.m
2 % @brief Normalizes a matrix to be a valid stochastic matrix
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Normalizes a matrix to be a valid stochastic matrix
9 %
10 % @details
11 % Rescales rows to sum to 1. Rows with zero sum are set to have 1 at the diagonal.
12 %
13 % @par Syntax:
14 % @code
15 % P = dtmc_makestochastic(P)
16 % @endcode
17 %
18 % @par Parameters:
19 % <table>
20 % <tr><th>Name<th>Description
21 % <tr><td>P<td>Input matrix
22 % </table>
23 %
24 % @par Returns:
25 % <table>
26 % <tr><th>Name<th>Description
27 % <tr><td>P<td>Valid stochastic transition matrix
28 % </table>
29%}
30function P=dtmc_makestochastic(P)
31for i=1:size(P,1)
32 if sum(P(i,:))>0
33 P(i,:)=P(i,:)/sum(P(i,:));
34 P(i,i)=min(max(0,1-(sum(P(i,:))-P(i,i))),1);
35 else
36 P(i,:)=0;
37 P(i,i)=1;
38 end
39end
40end