Main Content

rank

Rank of symbolic matrix

Description

k = rank(A) returns the rank of symbolic matrix A.

example

Examples

collapse all

Create a symbolic matrix.

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

(abcd)

Determine the rank of the matrix.

k = rank(A)
k = 
2

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 n is 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.

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 = 

(1-sin(x)cos(x)cos(x)sin(x)+1)

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 cos2(x)+sin2(x)=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 = 

(1-sin(3)cos(3)cos(3)sin(3)+1)

k2 = rank(A2)
k2 = 
2

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

A3 = vpa(A2)
A3 = 

(0.85887999194013277789925519719189-0.98999249660044545727157279473126-0.989992496600445457271572794731261.1411200080598672221007448028081)

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 = 

(cos(ϕ)cos(ϕ-2π3)cos(ϕ-4π3)sin(ϕ)sin(ϕ-2π3)sin(ϕ-4π3)111)

k = rank(B)
k = 
2
detB = simplify(det(B))
detB = 

-332

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 = 

(cos(ϕ)-cos(ϕ+π3)-sin(ϕ+π6)sin(ϕ)-sin(ϕ+π3)cos(ϕ+π6)111)

k2 = rank(B2)
k2 = 
3

Here, rank returns the correct result for the simplified 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 = 

(1s0s2+2s12s02021)

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 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 Ax=b with n variables in x has at least one solution if and only if the rank of the coefficient matrix A is equal to the rank of the augmented matrix [A|b]. If the system has solutions, they form an affine subspace of Rn of dimension n-rank(A). In other words, the solution is unique if and only if n=rank(A). Otherwise, the system has infinitely many solutions (the system is underdetermined) and the solutions have k=n-rank(A) 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 = 

(1-103-11-202021)

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 = 

(1-i01-i1-2i02021)

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

collapse all

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

See Also

| | |