File Exchange

image thumbnail

Round to X

version 1.4.0.0 (3.72 KB) by W. Owen Brimijoin
Rounds the supplied N-D matrix to the values in a supplied array.

2 Downloads

Updated 02 Mar 2015

View Version History

View License

Need to round your data to a set of particular values? This function takes as its input the data you want to round, the values you want the data rounded /to/, and a rounding method (currently 'round', 'floor' and 'ceil', that each work the way their .m counterparts do).
The function is vectorized for efficiency.
round2x(data,roundvals,method)
Given an N-dimensional array of numbers, this function will round all the elements of the array to the nearest elements in the supplied vector or ND array.

Example:
data = rand(2,9)*10; %data that you want to round
roundvals = [-1 2 3.14 8]; %numbers you want to round the data to
rounded = ROUND2X(data,roundvals);

If no rounding method is chosen, the function acts like 'round.m' in terms of rounding direction. The following methods can also be called:
'round' (Default: round towards nearest)
'floor' (round towards -infinity)
'ceil' (round towards +infinity)

Example:
rounded = ROUND2X(data,roundvals,'floor');

Happy rounding!

Cite As

W. Owen Brimijoin (2021). Round to X (https://www.mathworks.com/matlabcentral/fileexchange/47178-round-to-x), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (6)

Alex

Stephen Cobeldick

Nice work. Very tidy code, and does exactly what it says on the tin.

I agree that the "fix" option does not really make much sense as I originally thought it did. You are correct that it could be quite counter intuitive.

I would give this submission six stars if I could: it is rare to see code on FEX that uses inbuilt functions really effectively: bsxfun and interp1 are perfectly applied in this submission.

Owen Brimijoin

@Stephen Cobeldick: a revised version has now been posted to the FileEx based on your recommendations. All of them have been implemented, save one: the addition of a 'fix' method. The reason it hasn’t been implemented is that I am not convinced it should be. I can certainly BE convinced, but here’s a scenario for you and the file exchange community to puzzle over:

data = 0.1;
roundvals = [-10, 5, 10];

The result of using a ‘fix’ method here would be for the data to be rounded away from zero, to 5.0, which is a bit counterintuitive to the user who has just typed in ‘fix!’

Within the round-to-x framework, ‘fix’ in essence serves two masters, rounding not towards zero, but rather toward the supplied value that has the smallest absolute value. I think it’s confusing, but I’d greatly appreciate thoughts on the subject. I am fully prepared to admit that there may be uses for this code behavior that I am not seeing.

Owen Brimijoin

Thank you, Stephen, for working this function over and finding the bits that are in sore need of improvement. I was hoping someone might do exactly this! Based on your helpful suggestions, a new version will be forthcoming as soon as I find a spare evening to work on it.

Stephen Cobeldick

A nice concept that has been tidily implemented using fully vectorized code. The H1 line, examples and a good help section make it easy to search for and use. Clearly documented and commented code make it easy to read through.

This submission is "almost there", but a few points detract from the overall quality:

* Although the author states that the rounding operations "each work the way their .m counterparts do", this is not true for the most critical test-case: an input of 0.5 and a rounding vector of [0,1] rounds down to zero, not up to one like "round" does.

* Use of <varargin> instead of three explicitly named arguments is a major drawback. Naming arguments explicitly means that the code-prompting in the command line and editor would show the names of the arguments, whereas currently it shows the not-very-informative "varargin". It would also simplify the input handling (in the switch statement), as it does not require re-assignment of the variables.

* It needs the addition of 'fix' as an option (this is not the same as floor!).

* Input checking could be improved, e.g. check that <roundvec> is really a vector (this matters, as the code assumes this!), check if <data> is numeric (ditto).

* Either include handling for complex values, or throw an error if the input data is complex.

* The addition of a "See Also" line: this should link to "Round", "Fix", "Floor", etc.

Chang hsiung

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Acknowledgements

Inspired by: roundtowardvec

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!