-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslopingStaircase.m
55 lines (40 loc) · 1.06 KB
/
slopingStaircase.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
function y = slopingStaircase(x,k,m)
n = numel(k);
if nargin == 2
if ismember(mod(n,3),[0 1])
p = ceil(2*n/3);
m = k((p+1):end);
k = k(1:p);
else
error('Sloping staircase must have a slope for every other knot');
end
end
k = sort(k);
n = numel(k);
assert(numel(m) == floor(n/2),'Sloping staircase must have a slope for every other knot');
y = zeros(size(x));
y(x < k(1)) = 0;
lastY = 0;
for ii = 1:n-1
idx = x >= k(ii) & x < k(ii+1);
if ~any(idx)
continue % TODO : warning?
end
if mod(ii,2) == 0
y(idx) = lastY;
continue
end
yi = lastY+m((ii+1)/2)*(x(idx)-k(ii));
lastY = yi(end);
y(idx) = yi;
end
idx = x >= k(end);
if mod(ii,2) == 1
y(idx) = lastY;
else
y(idx) = lastY+m(end)*(x(idx)-k(end));
end
if nargin < 4
return
end
end