MATLAB Examples

# roundfrac documentation

This function rounds values to multiples of a fraction.

## Syntax

```Xrounded = roundfrac(X,fraction)
Xrounded = roundfrac(X,fraction,option)```

## Description

Xrounded = roundfrac(X,fraction) rounds X to multiples of nearest fraction. If fraction is a scalar, it is applied to all values in X. If dimensions of fraction match dimensions of X, it is applied element-wise. If fraction is 1/3, values of X are rounded to the nearest one third. roundfrac(X,1) is equivalent to round(X).

Xrounded = roundfrac(X,fraction,option) specifies an option string as 'floor', 'ceil', 'fix', or 'round'. Default rounding option is 'round'.

## Examples

Create an array of values to round:

```x = [-1.4 -1.3 -1.1 0 1.1 1.3 1.4]; ```

Round values of x to the nearest third:

```roundfrac(x,1/3) ```
```ans = -1.3333 -1.3333 -1.0000 0 1.0000 1.3333 1.3333 ```

Round x down to the nearest third:

```roundfrac(x,1/3,'floor') ```
```ans = -1.6667 -1.3333 -1.3333 0 1.0000 1.0000 1.3333 ```

Round x toward zero to the nearest third:

```roundfrac(x,1/3,'fix') ```
```ans = -1.3333 -1.0000 -1.0000 0 1.0000 1.0000 1.3333 ```

Round values of x to the nearest multiple of 2.5:

```roundfrac(x,2.5) ```
```ans = -2.5000 -2.5000 0 0 0 2.5000 2.5000 ```

Round each element of x to a different fraction:

```roundfrac(x,[1/3 1/2 1/16 3/7 4/7 44 .2]) ```
```ans = -1.3333 -1.5000 -1.1250 0 1.1429 0 1.4000 ```

## Author Info:

This function was written by Chad A. Greene of the University of Texas Institute for Geophysics (UTIG) on January 6, 2015.