To find eigenvalues and eigenvectors of a matrix in R, you can use the eigen()
function. This function calculates the eigenvalues and eigenvectors of a square matrix.
In this example,
mat
using the matrix()
function. This matrix represents the coefficients of a system of linear equations.eigen()
function to calculate the eigenvalues and eigenvectors of the matrix mat
. We assign the result to a variable named eigen_res
.eigen_res
using the $values
attribute and assign them to a variable named eigenvalues
.eigen_res
using the $vectors
attribute and assign them to a variable named eigenvectors
.mat <- matrix(c(1, 2, 3, 2, 4, 5, 3, 5, 6), nrow = 3, byrow = TRUE)
eigen_res <- eigen(mat)
eigenvalues <- eigen_res$values
eigenvectors <- eigen_res$vectors
print('Eigenvalues:')
print(eigenvalues)
print('Eigenvectors:')
print(eigenvectors)
[1] "Eigenvalues:" [1] 11.3448143 0.1709152 -0.5157295 [1] "Eigenvectors:" [,1] [,2] [,3] [1,] -0.3279853 0.5910090 0.7369762 [2,] -0.5910090 -0.7369762 0.3279853 [3,] -0.7369762 0.3279853 -0.5910090
In this example,
sym_mat
using the matrix()
function. This matrix represents a symmetric system or set of equations.eigen()
function to calculate the eigenvalues and eigenvectors of the symmetric matrix sym_mat
. We assign the result to a variable named eigen_res_sym
.eigen_res_sym
using the $values
attribute and assign them to a variable named eigenvalues_sym
.eigen_res_sym
using the $vectors
attribute and assign them to a variable named eigenvectors_sym
.sym_mat <- matrix(c(6, 2, 1, 2, 3, 2, 1, 2, 6), nrow = 3, byrow = TRUE)
eigen_res_sym <- eigen(sym_mat)
eigenvalues_sym <- eigen_res_sym$values
eigenvectors_sym <- eigen_res_sym$vectors
print('Eigenvalues (Symmetric Matrix):')
print(eigenvalues_sym)
print('Eigenvectors (Symmetric Matrix):')
print(eigenvectors_sym)
[1] "Eigenvalues (Symmetric Matrix):" [1] 8.464102 5.000000 1.535898 [1] "Eigenvectors (Symmetric Matrix):" [,1] [,2] [,3] [1,] -0.6279630 7.071068e-01 0.3250576 [2,] -0.4597008 1.221245e-15 -0.8880738 [3,] -0.6279630 -7.071068e-01 0.3250576
In this tutorial, we learned How to Find Eigenvalues and Eigenvectors of a Matrix in R language with well detailed examples.