LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
ImageFromDPH.m
1% ImageFromDPH(alpha, A, outFileName, prec)
2%
3% Depicts the given discrete phase-type distribution,
4% and either displays it or saves it to file.
5%
6% Parameters
7% ----------
8% alpha : matrix, shape (1,M)
9% The initial probability vector of the discrete phase-
10% type distribution.
11% A : matrix, shape (M,M)
12% The transition probability matrix of the discrete phase-
13% type distribution.
14% outFileName : string, optional
15% If it is not provided, or equals to 'display', the
16% image is displayed on the screen, otherwise it is
17% written to the file. The file format is deduced
18% from the file name.
19% prec : double, optional
20% Transition probabilities less then prec are
21% considered to be zero and are left out from the
22% image. The default value is 1e-13.
23%
24% Notes
25% -----
26% The 'graphviz' software must be installed and available
27% in the path to use this feature.
28
29function img = ImageFromPH(alpha,A,outFileName,prec)
30
31 if ~exist('prec','var')
32 prec = 1e-13;
33 end
34
35 if ~exist('outFileName','var') || strcmp(outFileName,'display')
36 outputFile = '.result.png';
37 displ = true;
38 else
39 outputFile = outFileName;
40 displ = false;
41 end
42
43 inputFile = '.temp.dot';
44
45 fid = fopen(inputFile,'w');
46 fprintf(fid, 'digraph G {\n');
47 fprintf(fid, '\trankdir=LR;\n');
48 fprintf(fid, '\tnode [shape=circle,width=0.3,height=0.3,label=""];\n');
49
50 % nodes
51 for i=1:length(alpha)
52 fprintf(fid, '\tn%d [xlabel=<<i>%g</i>>];\n', i, alpha(i));
53 end
54
55 % transitions to a non-absorbing state
56 for i=1:length(alpha)
57 for j=1:length(alpha)
58 if abs(A(i,j))>prec
59 fprintf(fid, '\tn%d -> n%d [label="%g"];\n', i, j, A(i,j));
60 end
61 end
62 end
63
64 % transitions to the absorbing state
65 fprintf(fid, ['\tab [style=filled];\n']);
66 a = 1-sum(A,2);
67 for i=1:length(alpha)
68 if abs(a(i))>prec
69 fprintf(fid, '\tn%d -> ab [label="%g"];\n', i, a(i));
70 end
71 end
72 fprintf(fid,'}\n');
73 fclose(fid);
74
75 [~,~,ext] = fileparts(outputFile);
76 system(['dot -T', ext(2:end), ' ', inputFile, ' -o ', outputFile]);
77
78 delete (inputFile);
79
80 if displ
81 RGB = imread(outputFile);
82 figure('toolbar','none','units','pixel','position',[0,0,size(RGB,2)+100,size(RGB,1)+100]);
83 image(RGB);
84 axis image;
85 delete(outputFile);
86 end
87end
88