-
Notifications
You must be signed in to change notification settings - Fork 0
/
Waterfill.m
42 lines (39 loc) · 1.13 KB
/
Waterfill.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
function [ popt] = Waterfill( noises, ptotal )
%WATERFILL Summary of this function goes here
% Detailed explanation goes here
[noises_sort, sort_index] = sort(noises);
K = size(noises, 2);
if K == 0
popt = zeros(1,0); %Kind of null return;
else
prem = ptotal;
popt_sort = zeros(1,K);
j = 1;
mu = noises_sort(1);
%calculate water level
while(prem > 0)
if j == K %Fill all the water here
mu = mu + prem/j;
prem = 0;
else
delta = noises_sort(j + 1) - noises_sort(j);
if (delta * j > prem)
mu = mu + prem/j;
prem = 0;
else
mu = mu + delta;
prem = prem - delta * j;
end
j = j + 1;
end
end
for j=1:K
if (mu > noises_sort(j))
popt_sort(j) = mu - noises_sort(j);
else
popt_sort(j) = 0;
end
end
popt(sort_index) = popt_sort;
end
end