summaryrefslogtreecommitdiff
path: root/macros/shiftdata.sci
blob: 8817e246764d553c99fdf05aff427de5de6694c3 (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
function [y,perm,nshifts] = shiftdata(x,dim)
//Shifts data by rearranging dimensions
//Calling sequence
//[y,perm,nshifts]=shiftdata(x,dim)
//[y,perm,nshifts]=shiftdata(x)
//Parameters
//x
//A vector matrix or n-dimensional array
//dim
//The dimension to be shifted to the first column
//Description
//[y,perm,nshifts]=shiftdata(x,dim)
//Shifts the entries along dimension dim in x to the first column and returns the permutation vector in perm
//[y,perm,nshifts]=shiftdata(x)
//Shifts the entries along dimension dim in x to the first column and returns the number of shifts in nshifts
//Examples
// //When dim is specified:
//x=testmatrix('magi',3)
//x  =
// 
//    8.    1.    6.  
//    3.    5.    7.  
//    4.    9.    2.
//[y,perm,nshifts] = shiftdata(x,2)
//nshifts  =
// 
//     []
// perm  =
// 
//    2.    1.  
// y  =
// 
//    8.    3.    4.  
//    1.    5.    9.  
//    6.    7.    2.  
// //When dim is not specified:
//x=1:5
//x  =
// 
//    1.    2.    3.    4.    5.
// [y,perm,nshifts] = shiftdata(x)
//nshifts  =
// 
//    1.  
// perm  =
// 
//     []
// y  =
// 
//    1.  
//    2.  
//    3.  
//    4.  
//    5.
//See also
//permute
//unshiftdata
//Author
//Ankur Mallick
    funcprot(0);
    if(argn(2)<1|argn(2)>2)
        error('Incorrect number of input arguments.');
    elseif(argn(2)==1|size(dim)==0)
        perm=[];
        S=size(x);
        v=find(S>1,1);
        y=matrix(x,S(v:length(S)));
        nshifts=v-1;
    else
        S=size(x);
        perm=1:1:length(S);
        perm(dim)=[];
        perm=[dim perm];
        y=permute(x,perm);
        nshifts=[];
    end
endfunction