Code covered by the BSD License  

Highlights from
logarithmic rounding

3.0

3.0 | 2 ratings Rate this file 1 Download (last 30 days) File Size: 2.26 KB File ID: #24040

logarithmic rounding

by Phillip M. Feldman

 

08 May 2009 (Updated 13 May 2009)

does logarithmic rounding using any of 7 modes

| Watch this File

File Information
Description

roundl is a MATLAB function that does base-10 logarithmic rounding.

  Syntax:

y= roundl(x, mode, n)

The value y is x rounded to a number of the form a*10^n, where n is an integer and a is one of a small number of multipliers.

x, the number to be logarithmically rounded, must be positive.

mode (optional) specifies the rounding direction. If specified, mode must be 'u' (round up), 'd' (round down), or 'n' (round to nearest); 'n' is the default.

n (optional) specifies the number of values per decade and thus determines the allowed multipliers. Allowed values of n are 1-7. The default value for n is 3; n= 5 and n= 7 are not commonly used. The following table shows the set of multipliers for each value of n:

1: 1
2: 1, 3
3: 1, 2, 5
4: 1, 2, 3, 5
5: 1, 2, 3, 5, 7
6: 1, 1.5, 2, 3, 5, 7
7: 1, 1.5, 2, 3, 4, 6, 8

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
08 May 2009 John D'Errico

I quite liked the basic idea when I saw it. However, there are a couple of flaws I found. The first was a surprise to me. It is not vectorized?

roundl(rand(20,1)*100)
??? Error using ==> mpower
Matrix must be square.

Error in ==> roundl at 91
   if (10^log10x_frac <= sqrt( multiplier(n,i-1)*multiplier(n,i) ) )

So this takes only scalar input? You are kidding me, right? A silly thing, IMHO. Since this is absolutely trivial to vectorize, why not do so?

The help was quite reasonable in general. It was missing an H1 line. Well, actually, it HAS an H1 line, but in the wrong place. There is a blank comment line as the very first line of the help. This causes problems for the lookfor function, since it looks at the very first comment line in the help block. Why do you want lookfor to work? Lookfor is the tool that your user will use next month or next year when they forget the name of your function. But by leaving that first line blank, you just disabled lookfor.

I did find error checks that seemed reasonable. Mlint points out a couple of minor items, like the use of | instead of || in conditionals. Read the help for relop to learn the difference.

14 May 2009 John D'Errico

I see that the code has been updated to provide vectorized operation. As always, I should consider raising my rating.

First, I looked for an H1 line. No. Still a blank line as the very first line of the help. This is utterly silly, since the second line of the help is a perfectly serviceable H1 line. This disables lookfor. Interestingly, the help facility that automatically builds a contents file for directories using the first lines of the help blocks finds roundl. But lookfor is itself disabled here. So this problem has not been repaired.

How bout the vectorized question?

x = rand(1,5)*100
x =
    74.555 73.627 56.186 18.419 59.721

roundl(x)
ans =
   100 100 50 20 50

By default, roundl uses a rounding strategy, i.e., a rounding mode of 'n'. But it can round up or down, more like a ceiling or floor operation then. All of these operations work nicely, and a default appears to be supplied for the rounding mode if necessary. Or is it?

roundl(x,'n')
ans =
   100 100 50 20 50

roundl(x,'u')
ans =
   100 100 100 20 100

roundl(x,'d')
ans =
    50 50 50 10 50

Now try the code by adding the third argument. The third argument to roundl allows you to control the levels rounded to.

roundl(x,'n',7)
ans =
    80 80 60 20 60

No problem so far. But what happens if you don't wish to change the default for the rounding mode, only the default for n? The paradigm in MATLAB when you have variables that will take on a default is if the variable is left empty ([]), then use the default. Does this work? No. This fails. I tried these things:

roundl(x,[],7)
??? Error using ==> roundl at 120
Invalid rounding mode:

roundl(x,'',7)
??? Error using ==> roundl at 120
Invalid rounding mode:

I even tried this:

roundl(x,7)
??? Error using ==> roundl at 120
Invalid rounding mode: 

As I said, this fails to follow the standard MATLAB paradigm for default arguments, which makes the code not very friendly. If you wish to change the rounding level, you must also provide a default for the other parameter. It is a truly silly flaw, because is it really that difficult to write your code as:

if (nargin < 2) || isempty(n), mode= 'n'; end

versus the line as it was written?

if nargin < 2, mode= 'n'; end

Sorry, but it is easy to write friendly code, and it is easy to make the code compatible with lookfor. Given this newly found problem, a 3 rating still applies here.

Please login to add a comment or rating.
Updates
13 May 2009

Modified code so that input x can be an array of arbitrary shape; the output y will match the shape of x.

Tag Activity for this File
Tag Applied By Date/Time
round Phillip M. Feldman 08 May 2009 10:12:30
rounding Phillip M. Feldman 08 May 2009 10:12:30
logarithmic rounding Phillip M. Feldman 08 May 2009 10:12:30
base10 Phillip M. Feldman 08 May 2009 10:12:30

Contact us at files@mathworks.com