Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
Researchers and analysts who arrive from a calculator, Excel, R or Python usually search this phrase for one reason: MATLAB has no function called ln. The natural logarithm is log(x). This guide covers the call, the other logarithm functions, what happens at zero and below, and how to read the output.
How do you type ln in MATLAB?
You type log(x). MathWorks documents Y = log(X) as returning the natural logarithm ln(x) of each element in array X, and a MathWorks-hosted Answers reply states that MATLAB has no ln function: log computes the natural logarithm. Typing ln(5) raises an undefined-function error; log(5) returns the value.
What it is used for in research
Natural logarithms appear whenever data are skewed or multiplicative: log-transforming a response variable before a regression, computing log-likelihoods, converting growth rates, or linearising an exponential decay so a straight-line fit can be applied. MathWorks also publishes a “Fit Logarithmic Models” page in its Curve Fitting documentation for that last case. For the related setup tasks see generating evenly spaced vectors with linspace and solving equations in MATLAB.
Install or access
MATLAB is a MathWorks product, and log is a core function, so no toolbox is needed to call it. Access options and licensing are described on the MathWorks site (mathworks.com). The function is the same in the Command Window, scripts and live scripts.
Step-by-step: take the natural log in MATLAB
- Put your values in a variable, for example
x = [1 2 10]; - Call
y = log(x). The function is applied to each element and returns an array of the same size. - For a single number,
log(5)works the same way. - To check your understanding of the inverse,
exp(log(x))returnsx(to rounding), becauseexpis the inverse of the natural logarithm.
The input can be a scalar, vector, matrix, N-d array, table or timetable, according to the MathWorks log reference page.
What does the log() function do?
log returns the natural logarithm, the logarithm to base e, element by element. Applied to a matrix it transforms every entry independently; it does not compute a matrix logarithm. For that, MathWorks provides logm, a separate function documented as the matrix logarithm.
What makes ln “natural”?
The base is Euler’s number e (about 2.71828), the constant for which the exponential function ex has itself as its derivative. The natural logarithm is the inverse of that function, which is why it turns up in continuous growth, decay and likelihood calculations. In MATLAB you can obtain the constant with exp(1).
How to use log10 in MATLAB
log10(X) returns the base-10 (common) logarithm of each element. MathWorks gives log10(10) returning 1 as its example, and notes that log10(0) returns -Inf. For real input in the open interval (0, Inf) the result is real; negative or complex input gives a complex result. The same MathWorks Answers thread identifies log2 as the base-2 logarithm and log1p as the natural log of 1+x.
Output and how to read it
| Function | What it returns | Source |
|---|---|---|
log(x) |
Natural logarithm, ln(x), element-wise | MathWorks log reference |
log10(x) |
Base-10 logarithm, element-wise | MathWorks log10 reference |
log2(x) |
Base-2 logarithm | MathWorks Answers (staff reply) |
log1p(x) |
Natural logarithm of 1+x | MathWorks Answers (staff reply) |
reallog(x) |
Listed under log‘s alternatives for raising an error instead of returning a complex result |
MathWorks log reference |
logm(A) |
Matrix logarithm | MathWorks logm reference |
Because the output is element-wise, log([1 2 10]) gives a row vector with three values (0, about 0.6931, about 2.3026). To convert between bases, divide by the log of the target base: log(x)/log(10) equals log10(x) mathematically.
Logarithms in another base
The functions above cover bases e, 10 and 2. For any other base b, use the change-of-base identity, logb(x) = ln(x) / ln(b), which is a mathematical identity rather than a MATLAB-specific feature:
b = 7;
y = log(x) ./ log(b) % base-b logarithm of each element of x
For bases 2 and 10 prefer log2 and log10.
Small values: log1p instead of log(1+x)
When x is very close to zero, the GNU Octave manual documents log1p(x) as computing log(1 + x) accurately in the neighborhood of zero, which is the reason to call it instead of writing log(1 + x) yourself. Floating-point results also mean identities such as exp(log(x)) == x can fail by rounding, so compare with a tolerance rather than exact equality.
Troubleshooting
Undefined function ‘ln’
Replace ln with log. MATLAB’s convention differs from tools where log means base 10.
You get 0.0000 + 3.1416i instead of an error
For negative input, log returns a complex result: MathWorks shows log(-1) giving 0.0000 + 3.1416i. For complex z = u + iw the real part is the log of the magnitude and the imaginary part is the angle. MathWorks warns this can lead to unexpected results if used unintentionally. If a negative value signals bad data, call reallog, which errors instead, or filter your data first.
You get -Inf
A zero in the input produces -Inf (documented for log10(0)). Remove or offset zeros deliberately, and document the choice in your analysis, rather than letting -Inf flow into a fit.
Precision loss for tiny x in ln(1+x)
Replace log(1+x) with log1p(x); see the log1p note above.
Taking the log of an image or other array
log works on N-d arrays, but check the data type and zeros first, since image pixels commonly include 0.
Free alternatives and translation table
If you do not have a MATLAB licence, GNU Octave documents log(x) as computing the natural logarithm ln(x) for each element, and also provides log10, log2 and log1p. Octave’s documentation describes reallog as aborting with an error if any element would produce a complex result.
| Natural log in | Call |
|---|---|
| MATLAB | log(x) |
| GNU Octave | log(x) |
| Python (NumPy) | numpy.log(x); see NumPy log |
| R | log(x) (base e is the default) |
| Calculator or spreadsheet | the ln key or LN() |
One contrast matters when you port code between tools. For a negative real number, MATLAB returns a complex value, whereas the NumPy documentation says numpy.log yields nan and sets the invalid floating-point error flag for real input whose result cannot be expressed as a real number or infinity. NumPy documents np.log([1, np.e, np.e**2, 0]) returning [0., 1., 2., -inf], and for complex input it treats the function as analytic with a branch cut along [-inf, 0]. Code that relies on a nan from bad data in Python will therefore behave differently in MATLAB unless you use reallog.
Online calculators exist for quick checks, but for a reproducible analysis keep the call in a script so the transformation is recorded. Related MATLAB basics: matrices in MATLAB, integration in MATLAB and epsilon (eps) in MATLAB.
FAQ
How do you type ln in MATLAB?
Type log(x). There is no ln function.
How to use log10 in MATLAB?
Call log10(x); log10(10) returns 1.
What makes ln natural?
Its base is e, and it inverts the exponential function ex.
What does the log() function do?
It returns the natural logarithm of every element of its input, returning complex values for negative input.
How do I take a logarithm in any other base?
Use the change-of-base identity: log(x) ./ log(b) gives the base-b logarithm of each element. For bases 2 and 10 use log2(x) and log10(x).
Sources
- MathWorks,
logreference page: mathworks.com/help/matlab/ref/double.log.html (read 2026-11-07) - MathWorks,
log10reference page: mathworks.com/help/matlab/ref/double.log10.html (read 2026-11-07) - MathWorks Answers, “How can I calculate ln(x) in matlab code?”: mathworks.com/matlabcentral/answers/225896 (read 2026-11-07)
- GNU Octave manual, Exponents and Logarithms: docs.octave.org/latest/Exponents-and-Logarithms.html (read 2026-11-07)
- NumPy documentation,
numpy.log: numpy.org/doc/stable/reference/generated/numpy.log.html (read 2026-11-14)







