summaryrefslogtreecommitdiff
path: root/macros/seqperiod.sci
blob: aa51d069939735fc03ddd11e882906d3cdb565e7 (plain)
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
function [p,num]=seqperiod(x)
//Calculates the period of a sequence
//Calling Sequence
//[p,num]=seqperiod(x)
//Parameters
//x: A vector matrix or n-dimensional array
//Description
//[p,num]=seqperiod(x)
//Returns an integer p such that x(1:p) is the smallest subsequence that repeats in x
//The number of times the subsequence repeats is returned in num (may not be an integer)
//Repetitions may be incomplete at the end of the sequence but no breaks are permitted between repetitions
//If there is no subsequence that repeats in x then p=length(x)
//If x is a matrix or n-dimesnional array, the function operates along the first non-singleton dimension of x
//Examples
//x = [4 0 1 5; 
//     1 1 2 5; 
//     2 0 3 5; 
//     3 1 1 5];
//p = seqperiod(x)
//p  =
// 
//    4.    2.    3.    1.
//A=zeros(4,1,4);
//A(:,1,:)=x;
//p1=seqperiod(A);
//p1  =
//
//(:,:,1)
//
//4.  
//(:,:,2)
//
//2.  
//(:,:,3)
//
//3.  
//(:,:,4)
//
//1.  
//Authors
//Ankur Mallick
    funcprot(0);
    if(argn(2)~=1)
        error('Incorrect number of input arguments.');
    else
        S=size(x);
        S1=S(S>1);
        if(length(S1)==1)
            u=0,v=0;
            x=matrix(x,S1,1);
            for i=1:S1
                if(S1>=2*i)
                    L=ceil((S1-i)/i);
                    v=matrix(x(1:i)*ones(1,L),i*L,1);
                    u=x(i+1:S1);
                    v=v(1:length(u));
                else
                    u=x(i+1:S1);
                    v=x(1:length(u));
                end
                if(v==u|i==S1)
                    p=i;
                    num=S1/p;
                    break;
                end
            end
        else
            x1=squeeze(x);
            S2=size(x1);
            p=zeros(sum(x1,1)); //summing x along first dimension gives p a leading singleton dimensionn
            num=zeros(p); 
            for i=1:prod(S2(2:length(S2)))
                [p1,num1]=seqperiod(x1(:,i));
                p(i)=p1; //Linear indexing
                num(i)=num1; //Linear indexing
            end
            p=matrix(p,[1,S(2:length(S))]);
            num1=matrix(num,[1,S(2:length(S))]);
        end
    end
endfunction