creating a Matrix
##using Vectors and cbind() and rbind()
xr1 <- c( 6, 2, 10)
xr2 <- c(1, 3, -2)
x <- rbind(xr1, xr2) ## binds the vectors into rows of a matrix
x
## [,1] [,2] [,3]
## xr1 6 2 10
## xr2 1 3 -2
y <- cbind(xr1, xr2) ## binds the same vectors into columns of a matrix
y
## xr1 xr2
## [1,] 6 1
## [2,] 2 3
## [3,] 10 -2
x[2,1]##The elements in a matrix are indexed by their (Row, Column) positions
## xr2
## 1
## using vector and dim()
my.vector<- 1:12
dim(my.vector) <- c(3,4)
my.vector
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
dim(my.vector)
## [1] 3 4
## Using array() and dim attribute
my.array<- array(1:12, dim=c(3,4))
my.array
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
dim(my.array)## To get the dimensions of a matrix using dim()
## [1] 3 4
## using matrix() function
A <- matrix(c( 6, 1, 0, -3, -1, 2),3, 2, byrow = TRUE)
B <- matrix(c( 4, 2, 0, 1, -5, -1),3, 2, byrow = TRUE)
A
## [,1] [,2]
## [1,] 6 1
## [2,] 0 -3
## [3,] -1 2
B
## [,1] [,2]
## [1,] 4 2
## [2,] 0 1
## [3,] -5 -1