Search notes:

R: matrix (data structure)

A matrix is a two dimensional array.

Matrix operations

Matrix transposition: t()
Matrix inverse: solve()
Matrix multiplication: m1 %*% m2 (m1 * m2 is an element wise operation).
n*n diagonal (identity) matrix: diag()

TODO

Compare with the matrix class.

Converting a vector to a matrix and vice versa

This is an old example that should be polished, somehow …
vec <- c('foo', 'bar', 'baz', 1, 2, 3);

#
#  Create a matrix from a vector:
#
mat <- matrix(vec, ncol=2);
mat;
#
#  vec <- c('foo', 'bar', 'baz', 1, 2, 3);
#  mat <- matrix(vec, ncol=2);
#  mat;

#
#  Create a vector again from the matrix:
#
c(mat);
#
#  "foo" "bar" "baz" "1"   "2"   "3" 


#
#  Same thing, but using byrow=TRUE
#
mat <- matrix(vec, ncol=2, byrow=TRUE)
mat;
#
#       [,1]  [,2] 
#  [1,] "foo" "bar"
#  [2,] "baz" "1"  
#  [3,] "2"   "3"  

#
#  Using c on a matrix gets the element
#  by iterating over the columns from
#  left to right and then for each element
#  in the column from top to bottom
#
c(mat);
#
#  "foo" "baz" "2"   "bar" "1"   "3" 

See also

Create a matrix with sapply
is.matrix()
outer
The matrix class
Data frames vs matrices

Index