# nthroot

Real nth root of real numbers

## Syntax

## Description

## Examples

### Calculate Real Root of Negative Number

Find the real cube root of `-27`

.

nthroot(-27, 3)

ans = -3

For comparison, also calculate `(-27)^(1/3)`

.

(-27)^(1/3)

ans = 1.5000 + 2.5981i

The result is the complex cube root of `-27`

.

### Calculate Several Real Roots of Scalar

Create a vector of roots to calculate, `N`

.

N = [5 3 -1];

Use `nthroot`

to calculate several real roots of `-8`

.

Y = nthroot(-8,N)

`Y = `*1×3*
-1.5157 -2.0000 -0.1250

The result is a vector of the same size as `N`

.

### Element-wise Roots of Matrix

Create a matrix of bases, `X`

, and a matrix of nth roots, `N`

.

X = [-2 -2 -2; 4 -3 -5]

`X = `*2×3*
-2 -2 -2
4 -3 -5

N = [1 -1 3; 1/2 5 3]

`N = `*2×3*
1.0000 -1.0000 3.0000
0.5000 5.0000 3.0000

Each element in `X`

corresponds to an element in `N`

.

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. `X`

can be either a scalar or an array of the
same size as `N`

. The elements of `X`

must
be real.

**Data Types: **`single`

| `double`

| `table`

| `timetable`

`N`

— Roots to calculate

scalar | array, table, or timetable of same size as `X`

Roots to calculate, specified as a scalar or array, table, or timetable of the same size as
`X`

. The elements of `N`

must be real.
If an element in `X`

is negative, the corresponding element
in `N`

must be an odd integer.

**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, use`nthroot`

to obtain the real roots.

## Extended Capabilities

### Tall Arrays

Calculate with arrays that have more rows than fit in memory.

This 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™.

This function fully supports GPU arrays. 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 R2006a**

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

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

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