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%> \example{pnint}
25%> \include{lineno} example/math/pnint/main.m
26%> \output{pnint}
27%> \include{lineno} example/math/pnint/main.out.m
28%>
29%> \final{pnint}
30%>
31%> \author
32%> \AmirShahmoradi, April 23, 2017, 1:36 AM, Institute for Computational Engineering and Sciences (ICES), University of Texas at Austin<br>
33function whole = pnint(val)
34 whole = round(val);
35 residual = val - whole; % abs(residual) < .5 always holds.
36 urand = unifrnd(0, 1, size(val, 1), size(val, 2));
37 mask = 1 - abs(residual) < urand;
38 mask1 = mask & residual < 0;
39 mask2 = mask & residual >= 0;
40 whole(mask1(:)) = whole(mask1(:)) - 1;
41 whole(mask2(:)) = whole(mask2(:)) + 1;
42end
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...