-
Notifications
You must be signed in to change notification settings - Fork 2
/
pst_build_trans_mat.m
222 lines (164 loc) · 5 KB
/
pst_build_trans_mat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
function [F_MAT ALPHABET N PI]=pst_build_trans_mat(DATA,ORDER)
%pst_build_trans_mat takes the cell array of strings DATA
%and generates a series of transition matrices up to 9th order (10 dimensional matrix). The
%matrices are all uint16, so change the data type if any cell in your frequency table
%might exceed 65535
%
% [F_MAT ALPHABET N PI]=pst_build_trans_mat(DATA,ORDER)
%
% DATA
% cell array, where each element contains a string of alphanumeric characters indicating
% different symbols or syllables. Elements are interpreted as separate trials.
%
% ORDER
% maximum order frequency table to compute (default: 5)
%
% ALPHABET
% string of all unique symbols found in the data (i.e. the corpus)
%
% N
% vector of total entries for each order
%
% PI
% starting distribution
%
if nargin<2, ORDER=5; end
if ORDER<1, error('Order must be at least 1!'); end
if ORDER>9, error('Maximum order is 8, user selected %g',ORDER); end
[sequence ALPHABET]=pst_sequence_gen(DATA);
%ALPHABET=[ALPHABET ']']; % add our end delimiter
ncat=length(ALPHABET); % add the end as an additional category
F_MAT{1}=zeros(1,ncat,'uint16');
F_MAT{2}=zeros(ncat,ncat,'uint16');
PI=zeros(size(F_MAT{1}));
if ORDER>1, F_MAT{3}=zeros(ncat,ncat,ncat,'uint16'); end
if ORDER>2, F_MAT{4}=zeros(ncat,ncat,ncat,ncat,'uint16'); end
if ORDER>3, F_MAT{5}=zeros(ncat,ncat,ncat,ncat,ncat,'uint16'); end
if ORDER>4, F_MAT{6}=zeros(ncat,ncat,ncat,ncat,ncat,ncat,'uint16'); end
if ORDER>5, F_MAT{7}=zeros(ncat,ncat,ncat,ncat,ncat,ncat,ncat,'uint16'); end
if ORDER>6, F_MAT{8}=zeros(ncat,ncat,ncat,ncat,ncat,ncat,ncat,ncat,'uint16'); end
if ORDER>7, F_MAT{9}=zeros(ncat,ncat,ncat,ncat,ncat,ncat,ncat,ncat,ncat,'uint16'); end
if ORDER>8, F_MAT{10}=zeros(ncat,ncat,ncat,ncat,ncat,ncat,ncat,ncat,ncat,ncat,'uint16'); end
% reached largest variable size, could use memmapfile
for i=1:length(F_MAT)
N(i)=0;
end
for i=1:length(DATA)
%song=[ DATA{i} ']' ]; % add the end delimiter
song=[ DATA{i} ];
if length(song)>1
A=find(song(1)==ALPHABET);
PI(A)=PI(A)+1;
end
for j=1:length(song)
A=find(song(j)==ALPHABET);
F_MAT{1}(A)=F_MAT{1}(A)+1;
N(1)=N(1)+1;
end
for j=1:length(song)-1
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
F_MAT{2}(A,B)=F_MAT{2}(A,B)+1;
N(2)=N(2)+1;
end
if ORDER>1
for j=1:length(song)-2
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
C=find(song(j+2)==ALPHABET);
F_MAT{3}(A,B,C)=F_MAT{3}(A,B,C)+1;
N(3)=N(3)+1;
end
end
if ORDER>2
for j=1:length(song)-3
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
C=find(song(j+2)==ALPHABET);
D=find(song(j+3)==ALPHABET);
F_MAT{4}(A,B,C,D)=F_MAT{4}(A,B,C,D)+1;
N(4)=N(4)+1;
end
end
if ORDER>3
for j=1:length(song)-4
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
C=find(song(j+2)==ALPHABET);
D=find(song(j+3)==ALPHABET);
E=find(song(j+4)==ALPHABET);
F_MAT{5}(A,B,C,D,E)=F_MAT{5}(A,B,C,D,E)+1;
N(5)=N(5)+1;
end
end
if ORDER>4
for j=1:length(song)-5
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
C=find(song(j+2)==ALPHABET);
D=find(song(j+3)==ALPHABET);
E=find(song(j+4)==ALPHABET);
F=find(song(j+5)==ALPHABET);
F_MAT{6}(A,B,C,D,E,F)=F_MAT{6}(A,B,C,D,E,F)+1;
N(6)=N(6)+1;
end
end
if ORDER>5
for j=1:length(song)-6
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
C=find(song(j+2)==ALPHABET);
D=find(song(j+3)==ALPHABET);
E=find(song(j+4)==ALPHABET);
F=find(song(j+5)==ALPHABET);
G=find(song(j+6)==ALPHABET);
F_MAT{7}(A,B,C,D,E,F,G)=F_MAT{7}(A,B,C,D,E,F,G)+1;
N(7)=N(7)+1;
end
end
if ORDER>6
for j=1:length(song)-7
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
C=find(song(j+2)==ALPHABET);
D=find(song(j+3)==ALPHABET);
E=find(song(j+4)==ALPHABET);
F=find(song(j+5)==ALPHABET);
G=find(song(j+6)==ALPHABET);
H=find(song(j+7)==ALPHABET);
F_MAT{8}(A,B,C,D,E,F,G,H)=F_MAT{8}(A,B,C,D,E,F,G,H)+1;
N(8)=N(8)+1;
end
end
if ORDER>7
for j=1:length(song)-8
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
C=find(song(j+2)==ALPHABET);
D=find(song(j+3)==ALPHABET);
E=find(song(j+4)==ALPHABET);
F=find(song(j+5)==ALPHABET);
G=find(song(j+6)==ALPHABET);
H=find(song(j+7)==ALPHABET);
I=find(song(j+8)==ALPHABET);
F_MAT{9}(A,B,C,D,E,F,G,H,I)=F_MAT{9}(A,B,C,D,E,F,G,H,I)+1;
N(9)=N(9)+1;
end
end
if ORDER>8
for j=1:length(song)-9
A=find(song(j)==ALPHABET);
B=find(song(j+1)==ALPHABET);
C=find(song(j+2)==ALPHABET);
D=find(song(j+3)==ALPHABET);
E=find(song(j+4)==ALPHABET);
F=find(song(j+5)==ALPHABET);
G=find(song(j+6)==ALPHABET);
H=find(song(j+7)==ALPHABET);
I=find(song(j+8)==ALPHABET);
J=find(song(j+9)==ALPHABET);
F_MAT{10}(A,B,C,D,E,F,G,H,I,J)=F_MAT{10}(A,B,C,D,E,F,G,H,I,J)+1;
N(10)=N(10)+1;
end
end
end