Code covered by the BSD License  

Highlights from
Round with significant digits

4.5

4.5 | 7 ratings Rate this file 74 Downloads (last 30 days) File Size: 2.29 KB File ID: #26212

Round with significant digits

by François Beauducel

 

28 Dec 2009 (Updated 20 Mar 2010)

Rounds towards the nearest number with N significant digits.

| Watch this File

File Information
Description

This little function rounds a number (or the elements of a vector ot matrix) towards the nearest number with N significant digits.

Examples:
roundsd(0.012345,3) returns 0.0123
roundsd(12345,2) returns 12000
roundsd(12.345,4,'ceil') returns 12.35

This is a useful complement to Matlab's ROUND, ROUND10 and ROUNDN (Mapping toolbox), especially when dealing with data with a large variety of order of magnitudes.

I was about to upload this file when I found "sd_round.m" by Edward Zechmann (see aknowledgment), which has similar feature (and more). But, because my script is very different in coding (much much shorter), I decided to share it anyway.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Round to a Specified Number of Significant Digits

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (11)
17 Mar 2010 Daniel Armyr

I just wrote one of these and was anout to upload it. But since you had allready done it, there is no point. This file would get a very high mark by me for several reasons:
1) Simple function, no bloated features
2) Error checking.
3) Well formated documentation.

Unfortunately, your script doesn't handle 0. That pulls down the mark significantly.

17 Mar 2010 Daniel Armyr

I also noticed some mlint-messages that I fixed. The core of the code now looks like this:

if ~isnumeric(n) || numel(n) ~= 1 || n < 0 || mod(n,1) ~= 0
error('N argument must be a scalar positive integer.')
end

valuesZero = (x==0);
og = 10.^(floor(log10(abs(x)) - n + 1));
y = round(x./og).*og;
y(valuesZero) = 0;

22 Mar 2010 François Beauducel

Thanks Daniel for your time. I submitted an updated version that corrects the zero-value behaviour, and I added some options for the rounding method.
About the mlint messages, I kept my original syntax which is voluntarily backward compatible (with Matlab versions < 7).

24 Mar 2010 Oleg Komarov

I would mention in the see also line the other rounding functions you use.

I never used matlab < 7 so I don't know if this applies:
1) no need to use find: y(find(x==0)) = 0;
2) I would use switch case instead of feval.

Oleg

24 Mar 2010 François Beauducel

Thanks Oleg.
Yes, I will complete the "see also" function list in future updates.

1) you effectively need the "find" in Matlab versions < 7... and also single pipe for the OR test.
2) like you I have hesitated between "switch" and "feval". But the feval code is much shorter...

François.

25 Mar 2010 Oleg Komarov

I think your submission deserves a much better overall rating.

05 Oct 2010 Forrest Brett  
06 Oct 2010 Sunday  
06 Jan 2011 K E

So useful that it should be built in to the Matlab round function, as it is in other languages

15 Mar 2011 Brent Boehlert

Much appreciated -- thanks.

23 Dec 2011 K E  
Please login to add a comment or rating.
Updates
28 Dec 2009

works with negative numbers.

29 Dec 2009

works with negative numbers.

20 Mar 2010

- adds different methods for rounding;
- corrects a bug for 0 values (now produces 0 instead of NaN), thanks to Daniel Armyr's comment.

Tag Activity for this File
Tag Applied By Date/Time
digit François Beauducel 28 Dec 2009 15:39:41
precision François Beauducel 28 Dec 2009 15:39:42
round François Beauducel 28 Dec 2009 15:39:42
round changsha ?? 23 Mar 2010 04:04:24
round Stephan 01 Apr 2012 05:49:48

Contact us at files@mathworks.com