Actually, easy. No loop needed, and just one line of code. You are essentially looking to find an array, I'll call it Counts, such that Counts(i,j) is the number of times the number i was followed by the number j in the vector x1. Pretty much two lines of code.
Counts = accumarray([x1(1:end-1),x1(2:end)],ones(numel(x1)-1,1))
Counts =
25 5 2 3 0 0 0 0 0
4 29 5 1 3 0 0 0 0
5 3 33 0 0 3 0 0 1
1 0 0 25 1 3 11 1 0
0 4 0 5 32 6 0 3 0
0 1 5 2 6 29 1 0 3
0 0 0 5 2 0 36 6 6
0 0 0 1 7 0 4 24 2
0 0 0 0 0 6 3 4 28
So, a 1 was followed by a 1 exactly 25 times, but a 3 was followed by 9 EXACTLY once. The most common occurrence was the pair [7 7], which we saw 36 times. We can test quickly to verify that fact.
strfind(x1',[7 7])
ans =
Columns 1 through 31
14 15 62 68 69 70 71 72 81 95 96 101 102 121 122 201 213 286 287 288 289 290 291 292 308 309 310 311 312 330 331
Columns 32 through 36
332 333 338 339 347
Yep. 36 occasions where [7 7] was seen. Where did that lonely [3 9] pair arise?
strfind(x1',[3 9])
ans =
33
If the vector x1 included 0 or negative numbers, we would need a second line, utilizing the function unique.
0 件のコメント
サインイン to comment.