Code covered by the BSD License

### Highlights from logb

4.6
4.6 | 7 ratings Rate this file 14 Downloads (last 30 days) File Size: 1.56 KB File ID: #14866 Version: 1.2

# logb

### Brett Shoelson (view profile)

03 May 2007 (Updated )

Calculate the log of a number/matrix using a user-specified base.

File Information
Description

FUNCTION Y = LOGB(X,B)
logb: Calculates the logarithm of x to user-specified base b.

USAGE: y = logb(x,b)

(A trivial but useful function for arbitrary-base log calculations.)

Arguments:
Y: the base 'b' logarithm of input x
B: The base of the logarithmic calculation (default, e). Note that if B is non-scalar, it must be the same size as X. In that case, the log transformation will be element-by-element.

Examples:
1) To calculate the base 5 logarithm of 3:
y = logb(3,5)

2) To calculate element-wise the log of a non-scalar input using different bases:
y = logb(magic(3),[1 2 3; 4 5 6; 7 8 9]);

Acknowledgements

This file inspired Order Of Magnitude Of Number.

MATLAB release MATLAB 7.4 (R2007a)
09 Jul 2015 Brett Shoelson

### Brett Shoelson (view profile)

@Carl:
It always pays to see if someone else already posted a function on the File Exchange. :)
Thanks for the rating.

Comment only
09 Jul 2015 Carl Witthoft

### Carl Witthoft (view profile)

Amazing :-) . I wrote exactly the same code (functionally), right down to the default value of "b" .

16 Aug 2011 Liber Eleutherios

### Liber Eleutherios (view profile)

I had just written my own function and ... I've found yours. Trivial but handy - should be part of Matlab.

13 Jul 2009 Juliette Salexa

### Juliette Salexa (view profile)

As far as I understand, running a funciton like this is slower than if matlab had an internal function, since the internal funciton would be compiled into machine code ??

Comment only
27 Mar 2008 Ubgunner Gunner
05 Jan 2008 Brett Shoelson

Not to reflect on your specific suggestion, Christophe, but I'd argue that no, you can't/shouldn't check everything. First, that's a near impossibility; someone can always find a way to trigger unwanted behavior. And second, one can add so much overhead in error checking that writing and maintaining (and even calling) functions can become cumbersome. Rather, one should check for the common things, leave the rest to the user. At some point, it makes sense to rely on the underlying on MATLAB's built-in errors/warnings.

Comment only
08 May 2007 Christophe Lauwerys

I would add an additional check on the dimensionalities of x and b, to prevent an error when they are different.

~isscalar(b) && ~ndims(x)==ndims(b) && ~ all(size(x)==size(b))

On the other hand, where does it stop, you can't check everything I guess? Or can / should you?

Comment only
07 May 2007 John D'Errico

As I'd expected, Brett cleaned this up nicely. My thanks.

07 May 2007 Brett Shoelson

Okay...I've been (appropriately) taken to task for my poor programming practices. All suggestions and corrections from Yvan and John have now been implemented--thank you to both of you. Sorry for my laxity! (Truthfully, I was surprised this wasn't a built-in function, too.)

Comment only
07 May 2007 John D'Errico

I'll be honest, I would have sworn that this functionality would already be in Matlab. (Surprise!) Yes, its trivial to do, and yes, it would be a handy occasionally anyway.

I gave this a 4 instead of a 5 because I believe that it costs little for good code to have error checks on the parameters (and because I believe that Brett should know this.) For example, b should either be a scalar or a vector or array the same size as x.

As I read through the code again, a couple of other flaws are the lack of an H1 line, and a usage line that gets the name of the function itself wrong. Hmm. As I write this, I wonder if I should have rated this a 3. Tsk, tsk, tsk. 8-) I think that Brett will fix those flaws.

04 May 2007 Yvan Lengwiler

With the said improvements, it could now actually come in handy (even if, as the author notes, the function is trivial).

04 May 2007 Brett Shoelson

Both valid points, Yvan. (log may be infinitessimally faster and perhaps more accurate than log10.) And I hadn't considered 'b as a vector,' but there's certainly no harm in allowing it. I've posted modified code to reflect the suggested changes...thanks.

Comment only
04 May 2007 Yvan Lengwiler

Two problems:

1) log10 is an external function (defined in an m-file), as opposed to log or log2, so it is (I guess) slower to execute. It is probably also less precise (I suppose). Say 'type log10' at the command prompt to see the definition. It is therefore preferrble to use log or log2 to do the transformation.

2) The program will give a wrong result if b is also a vector. Try logb([1 2 3],[2 2 2]). It gives 0.8617, when in fact the correct result would be [0 1.0000 1.5850].

Solution: Use log(x)./log(b) instead of log10(x)/log10(b).

04 May 2007

Yvan's comments make good sense. This might be faster and more accurate using log rather than log10, and ./ allows for a vector of bases.

04 May 2010 1.1

21 Jun 2010 1.2

Updated M file and license text file copyright to The MathWorks, Inc.