Professional Documents
Culture Documents
Matrices are multidimensional vectors; it is a data structure for storing objects of the same type.
b. Replace all “x” in the following code and run line by line and inspect output:
# By columns
matrix(data, ncol = x, byrow = FALSE) # byrow = FALSE by default
# By rows
matrix(data, ncol = x, byrow = TRUE)
c. Create a matrix of two vectors using cbind() and rbind(). You can use the two vectors below.
x <- c(2, 7, 3, 6, 1)
y <- c(3, 7, 3, 5, 9)
A <- c(3, 1, 6, 1, 2, 9)
dim(A) <- c(3, 2)
2. Inspection
3. Manipulation
1
# Add column
A <- cbind(A, c(6, 1, 7))
# Add row
A <- rbind(A, c(6, 1))
4. Calculus
Since matrices are vectors with multiple dimensions, the same math operators as for vectors applies
for matrices as well.