version 1.2.0.1 (1.56 KB) by
Brett Shoelson

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

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

See also: log, log10, log2

Brett Shoelson (2021). logb (https://www.mathworks.com/matlabcentral/fileexchange/14866-logb), MATLAB Central File Exchange. Retrieved .

Created with
R2007a

Compatible with any release

- MATLAB > Graphics > 2-D and 3-D Plots > Line Plots > Log Plots >

**Inspired:**
Order of magnitude of number

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Brett Shoelson@Carl:

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

Thanks for the rating.

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

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

Juliette SalexaDoes 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 GunnerBrett ShoelsonNot 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 LauwerysI 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'ErricoAs I'd expected, Brett cleaned this up nicely. My thanks.

Brett ShoelsonOkay...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'ErricoI'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 LengwilerWith the said improvements, it could now actually come in handy (even if, as the author notes, the function is trivial).

Brett ShoelsonBoth 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 LengwilerTwo 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).