File Exchange

image thumbnail


version (1.56 KB) by Brett Shoelson
Calculate the log of a number/matrix using a user-specified base.


Updated 01 Sep 2016

View Version History

View License

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.)

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.

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]);

See also: log, log10, log2

Cite As

Brett Shoelson (2021). logb (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Brett Shoelson

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

Carl Witthoft

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


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

Juliette Salexa

Does the latest version of matlab still not have this ???
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 ??

Ubgunner Gunner

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.

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?

John D'Errico

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

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.)

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.

Yvan Lengwiler

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

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.

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).

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Order of magnitude of number

Community Treasure Hunt

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

Start Hunting!