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
A symmetric matrix is a square matrix that is equal to its transpose. In other words, for a symmetric matrix
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.
Transpose of a matrix is obtained by interchanging its rows and columns. In MATLAB, the transpose of a matrix
Let's see an example of taking the transpose of a matrix:
A_transpose = A'; % Using the ' operatorA_transpose_alt = transpose(A); % Using the transpose function
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:
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.
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.
if-else
statementThe if-else
statement works with the isequal()
function comparing the matrix with its transpose.
If a matrix,
Let’s implement these ways in the code widget below:
A = [1 0; 0 1];B = issymmetric(A); % Check if the matrix is symmetricif Bdisp('A is symmetric.');elsedisp('A is not symmetric.');endC = [1 2 3; 4 5 6];% Check if the matrix is symmetricif isequal(C, C')disp('C is symmetric.');elsedisp('C is not symmetric.');end
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