File: SmartVec.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (108 lines) | stat: -rw-r--r-- 3,741 bytes parent folder | download | duplicates (7)
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
function vect = SmartVec(start,slength,step,mode)
% vect = SmartVec(start,slength,step,mode)
% makes sequence that satisfies certain conditions.
% 
% START is a scalar or vector with starting values of an sequence
% SLENGTH is a scalar or vector (with the same length as START) and
%   indicates the length of all sequences or the length of the
%   corresponding sequence in START.
% STEP is optional and indicates the stepsize of the series. Default is 1
%   STEP can be a scalar and indicate the stepsize for all segments, or it
%   can be the same lenght as START if you want to provide a different
%   stepsize for each segment
% MODE is optional and has two options:
%   'neg'  : the produced sequence will satisfy  : diff([n n+1]) = -1 
%   'flat' : the produced sequence will satisfy  : diff([n n+1]) =  0
% if no mode, the produced sequence will satisfy : diff([n n+1]) =  1
% MODE can be the third input to the function if you do not wish to
% override the default stepsize
%
% examples:
%   SmartVec([1 5],3)
%   ans =
%        1     2     3     5     6     7
%   SmartVec([1 5],[3 5])
%   ans =
%        1     2     3     5     6     7     8     9
%   SmartVec([1 5],[3 5],'neg')
%   ans =
%        1     0    -1     5     4     3     2     1
%   SmartVec([1 5],[3 5],2,'neg')
%   ans =
%        1     -1   -3     5     3     1     -1    -3
%   SmartVec([1 5],3,'neg')
%   ans =
%        1     0    -1     5     4     3
%   SmartVec([1 5],[3 5],'flat')
%   ans =
%        1     1     1     5     5     5     5     5
%   SmartVec([1 5],3,'flat')
%   ans =
%        1     1     1     5     5     5

% DN 2008-04-21 Written it, extention of SmartVec
% DN 2008-09-05 Updated engine, no more meshgrid, less transposes needed
% DN 2010-08-01 Now supports different stepsize than 1 for vectors
% DN 2011-07-23 Now supports different stepsizes for each segment

% check input
psychassert(length(slength)==1 || length(slength)==length(start),'slength must be a scalar or a vector with the same length as start');
qplat   = false;
qneg    = false;
mult    = 1;
if nargin >= 3
    qhavemode = false;
    % backwards compatibility, mode can be third input as well
    if nargin==3 && ischar(step)
        mode = step;
        qhavemode = true;
    end
    if nargin==4 || qhavemode
        switch mode
            case 'neg'
                qneg    = true;
            case 'flat'
                qplat   = true;
            otherwise
                error('Mode "%s" not recognized.\nUse "neg" of "flat".',mode)
        end
    end
    if isnumeric(step) && ~isempty(step)
        mult = step;
    end
end
psychassert(length(mult)==1 || length(mult)==length(start),'step must be a scalar or a vector with the same length as start');
maxlen      = max(slength);
slength     = slength(:);

% do the work
start       = start(:)';
startmat    = start(ones(maxlen,1),:);

if ~qplat
    vec         = [0:maxlen-1]';
    vecmat      = vec(:,ones(1,length(start)));
    if any(mult~=1)
        if ~isscalar(mult)
            vecmat  = bsxfun(@times,mult,vecmat);
        else
            vecmat  = mult .* vecmat;
        end
    end
    if qneg
        vecmat  = -1*vecmat;
    end
else
    vecmat      = zeros(maxlen,length(start));
end
    
totmat      = startmat + vecmat;
vect        = totmat(:)';

% if slength is a vector, we have to trim parts of the output
if ~isscalar(slength) && length(unique(slength))>1
    MinInd  = cumsum([0 maxlen*ones(1,length(slength)-1)]).'+1;
    MaxInd  = num2cell(MinInd+slength-1);
    indcell = cellfun(@(a,b)a:b,num2cell(MinInd),MaxInd,'UniformOutput',false);
    vect    = vect([indcell{:}]);
end