1 %> @ingroup conversion maths
3 %>@brief Converts percents to number of units with extra care
5 %> Extra care is taken so that:
6 %> @arg no elements in output be zero
7 %> @arg sum of elements in output does not surpass
"total"
14 irerror('Sum of percentages must be <= 1!');
17 irerror('All percentages must be a positive non-zero!');
21 no_unitss = zeros(1, np);
23 % The strategy is to start giving units to the smallest percentages first
24 [vals, idxs] = sort(percs);
27 no_unitss(idxs(i)) = max(1, round(vals(i)*total)); % Care 1
29 sofar = sum(no_unitss);
30 temp = max(1, round(vals(i)*total)); % Care 1
32 % Care 2: If would blow, the guy due to receive the most will actually get the remainder
35 irerror('Number of units =%d is too small to split!', total);
37 no_unitss(idxs(i)) = temp;
39 no_unitss(idxs(i)) = temp;
function percs2no_unitss(in percs, in total)