How to check if a matrix is symmetric in MATLAB

A matrix is a two-dimensional array of numbers arranged in rows and columns. In MATLAB, matrices are fundamental data structures used for various numerical computations.

Let's see an example of how to create a matrix:

A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % Defines a 3x3 matrix

Symmetric matrix

A symmetric matrix is a square matrix that is equal to its transpose. In other words, for a symmetric matrixAA,A=AA = A'.

Let's see an example of how to create a symmetric matrix:

B = [1, 2, 3; 2, 4, 5; 3, 5, 6]; % Defines a 3x3 symmetric matrix

In the illustration below notice that in the symmetric matrix, the values are symmetric across the main diagonal, whereas in the nonsymmetric matrix, this symmetry property is not satisfied.

Symmetric matrix vs. nonsymmetric
Symmetric matrix vs. nonsymmetric

Transpose

Transpose of a matrix is obtained by interchanging its rows and columns. In MATLAB, the transpose of a matrixAAis obtained using the'operator or the transpose function.

Let's see an example of taking the transpose of a matrix:

A_transpose = A'; % Using the ' operator
A_transpose_alt = transpose(A); % Using the transpose function

Symmetry check in MATLAB

If we need to check whether a matrix is symmetric, we can compare it to its transpose. We can check it in MATLAB using the following ways:

Using issymmetric()

The issymmetric() function in MATLAB checks whether a given matrix is symmetric. It returns 1 if the matrix is symmetric, meaning it is equal to its own transpose, and 0 otherwise.

Syntax

The basic syntax of the function is as follows:

issymmetric(A)

Here, A is the matrix for which we want to check if it is symmetric or not.

Using if-else statement

The if-else statement works with the isequal() function comparing the matrix with its transpose.
If a matrix,AA, is symmetric, thenAAequals its transpose, i.e.,A=AA = A'.

Example

Let’s implement these ways in the code widget below:

A = [1 0; 0 1];
B = issymmetric(A); % Check if the matrix is symmetric
if B
disp('A is symmetric.');
else
disp('A is not symmetric.');
end
C = [1 2 3; 4 5 6];
% Check if the matrix is symmetric
if isequal(C, C')
disp('C is symmetric.');
else
disp('C is not symmetric.');
end

Explanation

In the code above:

  • Line 1: It creates a matrix A.

  • Line 2: It calls issymmetric() on A and stores it in B.

  • Lines 3–7: Here, we check if B is 0 or 1 and print the results.

  • Line 9: It creates a matrix, C.

  • Lines 11–15: Here, we check if isequal(C, C') and print the results.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved