R TUTORIAL, part 2.1: Matrices

Matrices

"Matrix" is the Latin word for womb. The origin of mathematical matrices has a long history. The term "matrix" in combinatorics was introduced in 1850 by the British mathematician James Joseph Sylvester (1814--1897), who also coined many mathematical terms or used them in "new or unusual ways" mathematically, such as graphs, discriminants, annihilators, canonical forms, minor, nullity, and many others.

 

Constructing Matrices


Enter a matrix
\[ {\bf A} = \begin{bmatrix} 1&2&3 \\ 4&5&6 \end{mbmatrix} , \]
either by rows
or by columns
Access an element (i.j) of matrix A type A[2,3]:
Access an element of matrix A using a single index: indices count down the first column, then down the second column, etc.
Build an m×n matrix of zeros
  
matrix(0,nrow=m,ncol=n) 
#  or just
matrix(0,m,n)
Build an n × n identity matrix
Build a diagonal n × n matrix A using elements of vector v as diagonal entries
Extract diagonal elements of matrix A
“Glue” two matrices A1 and A2 (with the same number of rows) side-by-side
“Stack” two matrices A1 and A2 (with the same number of columns) on top of each other
Extract column 2 of matrix A
 A[,2]
that gives the result as a vector. To make the result a m×1 matrix (which is a column-vector) instead, do
 A[,2,drop=FALSE]
Extract second row of matrixA
 A[2,]
that gives the result as a vector. To make the result a m×1 matrix (which is a column-vector) instead, do
 A[2,,drop=FALSE]

 

Example: The paraboloid example shows that any function with three variables can be visually represented using the plot and plotly functions.

Next, we utilize anothe rpackage plotly for creating interactive web-based graphs via the open source JavaScript:

Rgl is another package that is useful for 3d plotting. It is interactive; however, mac users must download XQuartz to use the rgl package: https://www.xquartz.org
     ■

 

Example: We plot a torus with the follwoing commands:

     ■
 rm(x,y)

 

  1. https://cran.r-project.org/web/packages/deSolve/vignettes/deSolve.pdf
  2. Soeraert, K., Cash, J., Mazzia, F., Solving differential equations in R, Springer, 2012.
  3. https://www.lehigh.edu/~wes1/R_PDE/R-Matlab_compare.pdf