-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvcdf.m
48 lines (40 loc) · 1.01 KB
/
invcdf.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
% evaluate inverse cumulative distribution fuction inferred from D for
% probabilities P. (D must contain at least two different values.)
function x = invcdf(D, P)
nd = length(D);
D = sort(D);
np = length(P);
P = P*nd; % float in [0,nd]
R = ceil(P); % int in [0, nd]
R = R + (R==0); % int in [1, nd], points in [0,1) are interpolated externally
L = zeros(np,1);
% for each query
for k = 1:np
r = R(k);
l = r;
% find last occurrence of pointed element
while r < nd && D(r) == D(r+1)
r = r + 1;
end
% find first occurrence of pointed element
while l > 1 && D(l - 1) == D(l)
l = l - 1;
end
if l == 1
% find last occurrence of following element
l = r;
r = r + 1;
while r < nd && D(r) == D(r+1)
r = r + 1;
end
else
% find last occurrence of previous element
l = l - 1;
end
R(k) = r;
L(k) = l;
end
% vectorized interpolation
d = (P-L)./(R-L);
x = (D(L) + (D(R) - D(L)).*d);
end