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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1739916328, '3.145.10.108', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/R/data-structures/matrix/index(113): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78