FUNCTION Y = LOGB(X,B)
logb: Calculates the logarithm of x to userspecified base b.
USAGE: y = logb(x,b)
(A trivial but useful function for arbitrarybase 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 nonscalar, it must be the same size as X. In that case, the log transformation will be elementbyelement.
Examples:
1) To calculate the base 5 logarithm of 3:
y = logb(3,5)
2) To calculate elementwise the log of a nonscalar input using different bases:
y = logb(magic(3),[1 2 3; 4 5 6; 7 8 9]);
See also: log, log10, log2
Brett Shoelson (2020). logb (https://www.mathworks.com/matlabcentral/fileexchange/14866logb), MATLAB Central File Exchange. Retrieved .
1.2.0.1  Updated license 

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

1.1.0.0  added copyright 

1.0.0.0  Addresses some goodprogrammingpractices suggestions made by John D'Errico. Better description, help, error checking. 

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. 
Inspired: Order of magnitude of number
Create scripts with code, output, and formatted text in a single executable document.
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.
Carl Witthoft (view profile)
Amazing :) . I wrote exactly the same code (functionally), right down to the default value of "b" .
Liber Eleutherios (view profile)
I had just written my own function and ... I've found yours. Trivial but handy  should be part of Matlab.
Juliette Salexa (view profile)
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 ??
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 builtin errors/warnings.
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?
As I'd expected, Brett cleaned this up nicely. My thanks.
Okay...I've been (appropriately) taken to task for my poor programming practices. All suggestions and corrections from Yvan and John have now been implementedthank you to both of you. Sorry for my laxity! (Truthfully, I was surprised this wasn't a builtin function, too.)
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.
With the said improvements, it could now actually come in handy (even if, as the author notes, the function is trivial).
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.
Two problems:
1) log10 is an external function (defined in an mfile), 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).