# rank

Rank of symbolic matrix

## Syntax

## Description

## Examples

### Rank of Symbolic Matrix

Create a symbolic matrix.

syms a b c d A = [a b; c d]

A =$$\left(\begin{array}{cc}a& b\\ c& d\end{array}\right)$$

Determine the rank of the matrix.

k = rank(A)

k = 2

### Rank of Symbolic Matrix Without Round-Off Errors

The symbolic `rank`

function returns the exact rank of a symbolic matrix when the rank calculation does not require any further simplification and does not have applied conditions or assumptions. In contrast, numeric rank calculations performed on floating-point matrices can be susceptible to round-off errors, which can yield inaccurate results. The exact symbolic calculation is useful for ill-conditioned matrices, such as the Hilbert matrix. The rank of a Hilbert matrix of order $\mathit{n}$ is $\mathit{n}$.

Find the rank of the Hilbert matrix of order `15`

numerically. Then convert the numeric matrix to a symbolic matrix using `sym`

and find the rank symbolically.

H = hilb(15); k1 = rank(H)

k1 = 12

k2 = rank(sym(H))

k2 = 15

The symbolic calculation returns the correct rank of `15`

. However, the numeric calculation returns an inaccurate rank of `12`

due to round-off errors.

### Inaccurate Rank Calculation That Requires Simplification

The `rank`

function can overestimate or underestimate the rank of a symbolic matrix if the calculation requires further simplification.

Find the rank of the following square matrix and its determinant.

```
syms x
A = [1 - sin(x) cos(x);
cos(x) 1 + sin(x)]
```

A =$$\left(\begin{array}{cc}1-\mathrm{sin}\left(x\right)& \mathrm{cos}\left(x\right)\\ \mathrm{cos}\left(x\right)& \mathrm{sin}\left(x\right)+1\end{array}\right)$$

k = rank(A)

k = 2

detA = simplify(det(A))

`detA = $$0$$`

A square matrix has a full rank if and only if its determinant is nonzero. In this case, `rank`

overestimates the rank of the matrix and returns the full rank `2`

.

Although the matrix elements are already simplified, `rank`

returns an overestimated result because it does not apply the required trigonometric identity ${\mathrm{cos}}^{2}\left(\mathit{x}\right)+{\mathrm{sin}}^{2}\left(\mathit{x}\right)=1$ to calculate the exact rank of the matrix. For this reason, `rank`

returns an overestimated result even after substituting a value for the variable in the symbolic matrix.

A2 = subs(A,x,3)

A2 =$$\left(\begin{array}{cc}1-\mathrm{sin}\left(3\right)& \mathrm{cos}\left(3\right)\\ \mathrm{cos}\left(3\right)& \mathrm{sin}\left(3\right)+1\end{array}\right)$$

k2 = rank(A2)

k2 = 2

You can further convert this matrix to variable precision and find its rank.

A3 = vpa(A2)

A3 =$$\left(\begin{array}{cc}0.85887999194013277789925519719189& -0.98999249660044545727157279473126\\ -0.98999249660044545727157279473126& 1.1411200080598672221007448028081\end{array}\right)$$

k3 = rank(A3)

k3 = 1

Here, `rank`

returns the correct result for the variable-precision matrix.

Next, find the rank of the following square matrix and its determinant.

```
syms phi
B = [cos(phi) cos(phi - 2*pi/3) cos(phi - 4*pi/3);
sin(phi) sin(phi - 2*pi/3) sin(phi - 4*pi/3);
1 1 1]
```

B =$$\left(\begin{array}{ccc}\mathrm{cos}\left(\varphi \right)& \mathrm{cos}\left(\varphi -\frac{2\hspace{0.17em}\pi}{3}\right)& \mathrm{cos}\left(\varphi -\frac{4\hspace{0.17em}\pi}{3}\right)\\ \mathrm{sin}\left(\varphi \right)& \mathrm{sin}\left(\varphi -\frac{2\hspace{0.17em}\pi}{3}\right)& \mathrm{sin}\left(\varphi -\frac{4\hspace{0.17em}\pi}{3}\right)\\ 1& 1& 1\end{array}\right)$$

k = rank(B)

k = 2

detB = simplify(det(B))

detB =$$-\frac{3\hspace{0.17em}\sqrt{3}}{2}$$

The determinant of the matrix is nonzero, so the matrix has a full rank of `3`

. In this case, `rank`

underestimates the rank of the matrix and returns `2`

.

Rewrite the matrix using `simplify`

and find its rank.

B2 = simplify(B)

B2 =$$\left(\begin{array}{ccc}\mathrm{cos}\left(\varphi \right)& -\mathrm{cos}\left(\varphi +\frac{\pi}{3}\right)& -\mathrm{sin}\left(\varphi +\frac{\pi}{6}\right)\\ \mathrm{sin}\left(\varphi \right)& -\mathrm{sin}\left(\varphi +\frac{\pi}{3}\right)& \mathrm{cos}\left(\varphi +\frac{\pi}{6}\right)\\ 1& 1& 1\end{array}\right)$$

k2 = rank(B2)

k2 = 3

Here, `rank`

returns the correct result for the simplified matrix.

### Solve System of Equations to Determine Rank of Matrix

The `rank`

function can return an inaccurate result if the rank calculation requires further simplification or has applied conditions or assumptions. In this case, you can investigate the solutions of a system of equations defined by the input matrix to better understand the rank of the matrix. For example, create a matrix and calculate its rank.

```
syms s
A = [1 s 0 2+s^2;
s 1 2*s 0;
2 0 2 1]
```

A =$$\left(\begin{array}{cccc}1& s& 0& {s}^{2}+2\\ s& 1& 2\hspace{0.17em}s& 0\\ 2& 0& 2& 1\end{array}\right)$$

k = rank(A)

k = 3

Here, `rank`

returns the full rank, which is 3. However, this result may not apply to all values of `s`

.

To better understand the rank of the matrix, solve the system of equations $\mathbf{Ax}=0$ by using `solve`

and setting the `ReturnConditions`

name-value argument to `true`

to return any parameters and conditions of the solutions. According to the Rouché–Capelli theorem, a system of equations $\mathbf{Ax}=\mathbf{b}$ with $\mathit{n}$ variables in $\mathbf{x}$ has at least one solution if and only if the rank of the coefficient matrix $\mathbf{A}$ is equal to the rank of the augmented matrix $\left[\mathbf{A}|\mathbf{b}\right]$. If the system has solutions, they form an affine subspace of $${\mathbb{R}}^{n}$$ of dimension $\mathit{n}-\mathrm{rank}\left(\mathit{A}\right)$. In other words, the solution is unique if and only if $\mathit{n}=\mathrm{rank}\left(\mathit{A}\right)$. Otherwise, the system has infinitely many solutions (the system is *underdetermined*) and the solutions have $\mathit{k}=\mathit{n}-\mathrm{rank}\left(\mathit{A}\right)$ free parameters.

syms x [4 1] sols = solve(A*x == 0,x,"ReturnConditions",true)

`sols = `*struct with fields:*
x1: -2*z
x2: -s*z
x3: (3*z)/2
x4: z
parameters: z
conditions: s ~= -1 & s ~= 1 & s ~= -1i & s ~= 1i

Here, `solve`

returns nonunique solutions for the four variables in `x`

with one free parameter, where specific conditions must be satisfied by the solutions of `x`

. Therefore, if all the conditions for the variable `s`

in the solutions are satisfied, the rank of the matrix is 4 – 1 = 3.

Next, investigate the condition where `s = -1`

. Substitute this value in the matrix `A`

and solve another system of equations defined by the substituted matrix.

A2 = subs(A,s,-1)

A2 =$$\left(\begin{array}{cccc}1& -1& 0& 3\\ -1& 1& -2& 0\\ 2& 0& 2& 1\end{array}\right)$$

`sols2 = solve(A2*x == 0,x,"ReturnConditions",true)`

`sols2 = `*struct with fields:*
x1: -2*z
x2: z
x3: (3*z)/2
x4: z
parameters: z
conditions: symtrue

Here, `solve`

returns different solutions for the substituted matrix. The solutions have one free parameter, which is `z`

, and has no additional conditions. Therefore, the rank of the substituted matrix where `s = -1`

is 4 – 1 = 3.

Next, investigate the condition where `s = -1i`

. Substitute this value in the matrix `A`

and solve another system of equations defined by the substituted matrix.

A3 = subs(A,s,-1i)

A3 =$$\left(\begin{array}{cccc}1& -\mathrm{i}& 0& 1\\ -\mathrm{i}& 1& -2\hspace{0.17em}\mathrm{i}& 0\\ 2& 0& 2& 1\end{array}\right)$$

`sols3 = solve(A3*x == 0,x,"ReturnConditions",true)`

`sols3 = `*struct with fields:*
x1: - z - z1/2
x2: z*1i - (z1*1i)/2
x3: z
x4: z1
parameters: [z z1]
conditions: symtrue

Here, `solve`

returns different solutions with two free parameters, which are `z`

and `z1`

, and no additional conditions. Therefore, the rank of this substituted matrix where `s = -1i`

is 4 – 2 = 2.

## Input Arguments

`A`

— Input

number | vector | matrix | symbolic number | symbolic vector | symbolic matrix

Input, specified as a number, vector, or matrix or a symbolic number, vector, or matrix.

## Limitations

The

`rank`

function returns the exact rank of a symbolic matrix only if the rank calculation does not require any further simplification and does not have applied conditions or assumptions. If further simplification, conditions, or assumptions are needed, then`rank`

can return an inaccurate result. In this case, to better understand the rank of the input matrix, you can investigate the solutions of a system of equations defined by the matrix using`solve`

. For example, see Solve System of Equations to Determine Rank of Matrix.

## References

[1] Shafarevich, Igor R., and
Alexey O. Remizov. *Linear Algebra and Geometry*.
Berlin, Heidelberg: Springer, 2013.

## Version History

**Introduced before R2006a**

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