numcol=5 numrow=3 mat=matrix(rnorm(numcol*numrow), nrow=numrow, ncol=numcol); lower.tri(mat, diag = TRUE) upper.tri(mat, diag = TRUE) matL=mat matU=mat ## lower and upper triangular matrices: matL[!lower.tri(matL, diag = TRUE)]=0 ## in matlab: tril(mat) matU[!upper.tri(matU, diag = TRUE)]=0 ## in matlab: triu(mat) ## alternative lower and upper triangular matrix calculation, which uses the same functions with MATLAB: ## install the pracma package: install.packages("pracma") ## run once ## load the library: library(pracma) ## run each time, whenever you use this package tril(mat) triu(mat) ## take transpose: t(mat) ## in matlab: mat' ## find determinant: det(mat) ## in matlab: det(mat) function ## ERROR: 'x' must be a square matrix ## take inverse: solve(mat) ## in matlab: inv(mat) function ## ERROR: 'x' must be a square matrix ## let us create a square matrix squaremat=matrix(rnorm(25), nrow=5, ncol=5) ## det(squaremat) solve(squaremat) ## matrix addition and subtraction: mat1=matrix(rnorm(15), nrow=5, ncol=3) mat2=matrix(rnorm(15), nrow=5, ncol=3) mat1 mat2 mat1 + mat2 mat1 - mat2 ## matrix multiplication: mat1 %*% mat2 ## error because of the size mismatch: "Error : non-conformable arguments" mat3=matrix(rnorm(12), nrow=3, ncol=4) mat1 %*% mat3 mat2 %*% mat3 BAP-Özgeçmiş