ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pnint.m
Go to the documentation of this file.
1%> \brief
2%> Generate and return the **probabilistically-rounded** to the nearest integer value of the input real number.
3%>
4%> \details
5%> Unlike the intrinsic Fortran `nint()` function, the procedures of this generic
6%> interface round the input real number to the nearest integer value **probabilistically**.<br>
7%> The closer the input `val` is to its `nint(val)` result, the more likely it will be rounded to it.<br>
8%> See below for example usage.<br>
9%>
10%> \param[in] val : The input scalar or array of the same rank as other array-like arguments,
11%> containing the number to be rounded probabilistically.<br>
12%>
13%> \return
14%> `whole` : The output scalar or array of the same rank and shape as the input arguments,
15%> of default `integer` kind, containing the **probabilistically-rounded** input value to the nearest integer.<br>
16%>
17%> \interface{pnint}
18%> \code{.m}
19%>
20%> whole = pm.math.pnint(val);
21%>
22%> \endcode
23%>
24%> \impure
25%>
26%> \elemental
27%>
28%> \example{pnint}
29%> \include{lineno} example/math/pnint/main.m
30%> \output{pnint}
31%> \include{lineno} example/math/pnint/main.out.m
32%>
33%> \final{pnint}
34%>
35%> \author
36%> \AmirShahmoradi, April 23, 2017, 1:36 AM, Institute for Computational Engineering and Sciences (ICES), University of Texas at Austin
37function whole = pnint(val)
38 whole = round(val);
39 residual = val - whole; % abs(residual) < .5 always holds.
40 urand = unifrnd(0, 1, size(val, 1), size(val, 2));
41 mask = 1 - abs(residual) < urand;
42 mask1 = mask & residual < 0;
43 mask2 = mask & residual >= 0;
44 whole(mask1(:)) = whole(mask1(:)) - 1;
45 whole(mask2(:)) = whole(mask2(:)) + 1;
46end
function abs(in path, in style)
Return the Get absolute canonical path of a file or folder.
function pnint(in val)
Generate and return the probabilistically-rounded to the nearest integer value of the input real numb...