Matrix Construction
There are various ways to construct a matrix. When we construct a matrix directly with data elements, the matrix content is filled along the column orientation by default. For example, in the following code snippet, the content of B is filled along the columns consecutively.
+ c(2, 4, 3, 1, 5, 7),
+ nrow=3,
+ ncol=2)
> B # B has 3 rows and 2 columns
[,1] [,2]
[1,] 2 1
[2,] 4 5
[3,] 3 7
Transpose
We construct the transpose of a matrix by interchanging its columns and rows with the function t .
Combining Matrices
The columns of two matrices having the same number of rows can be combined into a larger matrix. For example, suppose we have another matrix C also with 3 rows.
Then we can combine the columns of B and C with cbind.
Similarly, we can combine the rows of two matrices if they have the same number of columns with the rbind function.
+ c(6, 2),
+ nrow=1,
+ ncol=2)
> D # D has 2 columns
[,1] [,2]
[1,] 6 2
> rbind(B, D)
[,1] [,2]
[1,] 2 1
[2,] 4 5
[3,] 3 7
[4,] 6 2
Deconstruction
We can deconstruct a matrix by applying the c function, which combines all column vectors into one.