Construct a Diagonal Matrix in R Programming - diag() Function Last Updated : 01 Sep, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report diag() function in R Language is used to construct a diagonal matrix. Syntax: diag(x, nrow, ncol)Parameters: x: value present as the diagonal elements. nrow, ncol: number of rows and columns in which elements are represented. Example 1: Python3 # R program to illustrate # diag function # Calling the diag() function with # some number which will act as # rows as well as columns number diag(3) diag(5) Output: [, 1] [, 2] [, 3] [1, ] 1 0 0 [2, ] 0 1 0 [3, ] 0 0 1 [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 0 0 0 0 [2, ] 0 1 0 0 0 [3, ] 0 0 1 0 0 [4, ] 0 0 0 1 0 [5, ] 0 0 0 0 1 Example 2: Python3 # R program to illustrate # diag function # Calling the diag() function diag(5, 2, 3) diag(10, 3, 3) Output: [, 1] [, 2] [, 3] [1, ] 5 0 0 [2, ] 0 5 0 [, 1] [, 2] [, 3] [1, ] 10 0 0 [2, ] 0 10 0 [3, ] 0 0 10 Comment More infoAdvertise with us Next Article Get or Set Dimensions of a Matrix in R Programming - dim() Function K Kanchan_Ray Follow Improve Article Tags : R Language R Matrix-Function Similar Reads Get or Set Dimensions of a Matrix in R Programming - dim() Function The dim() function in R is used to get or set the dimensions of an object. This function is particularly useful when working with matrices, arrays, and data frames. Below, we will discuss how to use dim() to both retrieve and set dimensions, with examples for better understanding.Syntax: dim(x)Param 2 min read Finding Inverse of a Matrix in R Programming - inv() Function inv() function in R Language is used to calculate inverse of a matrix. Note: Determinant of the matrix must not be zero Syntax: inv(x) Parameters: x: Matrix Example 1: Python3 1== # R program to calculate # inverse of a matrix # Loading library library(matlib) # Create 3 different vectors. a1 <- 1 min read Find Eigenvalues and Eigenvectors of a Matrix in R Programming - eigen() Function eigen() function in R Language is used to calculate eigenvalues and eigenvectors of a matrix. Eigenvalue is the factor by which a eigenvector is scaled. Syntax: eigen(x) Parameters: x: Matrix Example 1: Python3 1== # R program to illustrate # Eigenvalues and eigenvectors of matrix # Create a 3x3 mat 1 min read Calculate Trace of a Matrix in R Programming - tr() Function tr() function in R Language is used to calculate the trace of a matrix. Trace of a matrix is the sum of the values on the main diagonal(upper left to lower right) of the matrix. Syntax: tr(x) Parameters: x: Matrix Example 1: Python3 1== # R program to calculate # trace of a matrix # Loading library 1 min read Convert an Object into a Matrix in R Programming - as.matrix() Function The as.matrix() function within R converts objects of various classes into a matrix. This can be helpful to work with structures of various data that can be converted into the matrix structure so that it becomes easier to analyze.Syntax: as.matrix(x)Parameters: x: Object to be convertedExample 1: Co 2 min read Scale the Columns of a Matrix in R Programming - scale() Function The scale() function in R is used to center and/or scale the columns of a numeric matrix. It helps standardize data by transforming it to have a mean of 0 and standard deviation of 1 .Syntax scale(x, center = TRUE, scale = TRUE) Parameters: x: represents numeric matrix .center: represents either log 2 min read Like