Ref: http://tryr.codeschool.com/levels/3/challenges/25
Via the matrix
function.
Create matrixes with the matrix(default_value, num_rows,
num_columns)
or matrix(vector_of_values, num_rows,
num_columns)
function calls.
By reshaping an existing vector.
> x = 1:8 > x [1] 1 2 3 4 5 6 7 8 > dim(x) NULL > dim(x) = c(2, 4) > x [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 > x[1] [1] 1 > x[2] [1] 2 > x[1,] [1] 1 3 5 7 > x[,1] [1] 1 2 > x[2,c(2, 4)] [1] 4 8 > x[,c(2, 4)] [,1] [,2] [1,] 3 7 [2,] 4 8 > dim(x) = c(2, 2, 2) > x , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 # Out of bounds. > x[1234] [1] NA > x[12, 12] Error in x[12, 12] : incorrect number of dimensions
volcano
: Built in matrix.
It's a 3D map of a dormant New Zealand volcano as an 87x61 matrix with elevation values.
contour(volcano)
# expand=0.3 limits the vertical expansion to 1/5th. # Otherwise, the top of the volcano will be at the top # of the viewport and it looks weird. persp(volcano, expand=0.2)
# Plots a heatmap. image(volcano)