nthroot
Real nth root of real numbers
Syntax
Description
Examples
Real Root of Negative Number
Find the real cube root of -27
.
nthroot(-27,3)
ans = -3
For comparison, calculate (-27)^(1/3)
. The result is the complex cube root of -27
.
(-27)^(1/3)
ans = 1.5000 + 2.5981i
Real Roots of Scalar
Calculate several real nth roots of -8
.
N = [5 3 -1]; Y = nthroot(-8,N)
Y = 1×3
-1.5157 -2.0000 -0.1250
Real Roots of Vector
Create a row vector of bases, X
, and a column vector of roots to calculate, N
.
X = [4 -3 -5]; N = [1; -1; 3];
Calculate the real nth roots of the elements in X
. The result is a matrix containing all combinations of bases and roots. For example, Y(3,1)
is the 3rd root of 4.
Y = nthroot(X,N)
Y = 3×3
4.0000 -3.0000 -5.0000
0.2500 -0.3333 -0.2000
1.5874 -1.4422 -1.7100
Element-Wise Roots of Matrix
Create a matrix of bases, X
, and a matrix of roots to calculate, N
. Each element in X
corresponds to an element in N
.
X = [-2 -2 -2; 4 -3 -5]; N = [1 -1 3; 1/2 5 3];
Calculate the real nth roots of the elements in X
.
Y = nthroot(X,N)
Y = 2×3
-2.0000 -0.5000 -1.2599
16.0000 -1.2457 -1.7100
Except for the signs (which are treated separately), the result is comparable to abs(X).^(1./N)
. By contrast, you can calculate the complex roots using X.^(1./N)
.
Input Arguments
X
— Input array
scalar | vector | matrix | multidimensional array | table | timetable
Input array, specified as a scalar, vector, matrix, multidimensional array, table, or
timetable. The elements of X
must be real. Inputs
X
and N
must either be the
same size or have sizes that are compatible.
Data Types: single
| double
| table
| timetable
N
— Roots to calculate
scalar | vector | matrix | multidimensional array | table | timetable
Roots to calculate, specified as a scalar, vector, matrix, multidimensional array, table, or
timetable. The elements of N
must be real. If an
element in X
is negative, the corresponding element in
N
must be an odd integer. Inputs
X
and N
must either be the
same size or have sizes that are compatible.
If
X
is a multidimensional array,N
cannot be a table or timetable.If
X
is a table or timetable, thenN
can be the same data type asX
or a scalar, vector, matrix, or multidimensional array of a compatible size. If both inputs are tables or timetables, then see Rules for Table and Timetable Mathematics for the input requirements.
Data Types: single
| double
| table
| timetable
Tips
While
power
is a more efficient function for computing the roots of numbers, in cases where both real and complex roots exist,power
returns only the complex roots. In these cases, usenthroot
to obtain the real roots.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
nthroot
function fully supports tall arrays. For more information,
see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The nthroot
function
fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray
(Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006aR2023b: Support for implicit expansion of arguments
nthroot
supports implicit expansion of the input arguments
X
and N
. For more information on the
required input sizes, see Compatible Array Sizes for Basic Operations.
If your code relies on the errors that MATLAB® previously returned for mismatched sizes, particularly within a
try
/catch
block, then your code might
no longer catch those errors.
R2023a: Perform calculations directly on tables and timetables
The nthroot
function can calculate on all variables within a table or
timetable without indexing to access those variables. All variables must have data types
that support the calculation. For more information, see Direct Calculations on Tables and Timetables.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)