Class-18

WORKING WITH MATRICES IN R
X is a matrix of size 4 x 2
(4 rows by 2 columns)
WORKING WITH MATRICES IN R
X is a matrix of size 2 x 3
(2 rows by 3 columns)
Note the order how the matrix is filled
The data vector with entries c(1,2,0,0,0,3)
x1,1
x1,2
x1,3
x1,2
x2,2
x2,3
1,
2,
0,
0,
0,
1
0
0
2
0
3
3
WORKING WITH MATRICES IN R
X is a matrix of size 4 x 2
(4 rows by 2 columns)
A matrix is a 2-dimensional array of data. We can plot 2-dimensional data arrays
in R with the image() function using different colors for the entries in the matrix.
y
5
6
7
8
1
2
3
4
image(X)
x
Problems:
(1) rows are ordered along xaxis, columns ordered
along y-axis
WORKING WITH MATRICES IN R
X is a matrix of size 4 x 2
(4 rows by 2 columns)
A matrix is a 2-dimensional array of data. We can plot 2-dimensional data arrays
in R with the image() function using different colors for the entries in the matrix.
y
8
4
5 36
7
2
1
2
1
image(t(X))
87
(2) The transpose function t(X)
yields an up-side-down image
of the matrix
6
3
Problems:
4
5
x
WORKING WITH MATRICES IN R
To solve the problem and plot an image that orders the rows and columns exactly
as defined in our matrix X:
(1) Flip the transposed matrix such that
rows are ‘up-side-down’:
see updated scripts/myfunctions.R :
matrix_flip_v(X)
(2) And then use the transpose of the returned
(‘up-side-down’) matrix in the
image() plot function t(matrix_flip_v(X))
WORKING WITH MATRICES IN R
WORKING WITH MATRICES IN R
(1) In myfunctions.R there is a supporting
function matrix_image()
(2) Call res<-matrix_image(X)
(Note: the function returns the
transformed matrix)
1
5
2
6
3
7
4
8
WORKING WITH MATRICES IN R
demo_matrix_image.R
Download the updated scripts
myfunctions.R
demo_matrix_image.R
(data/labrador.ppm
http://www.atmos.albany.edu/facstaff/timm/ATM315spring14/R/scripts/
Slides from class 11
Vectors in R:
y<-c(x1,x2,x3,…xn)
What does it mean when two variables are forming a scatter
surrounding a linear regression line?
Mathemathics:
‘magnitude’ or ‘length’ of a vector
R-Code:
Another common notation for vector dot products
WORKING WITH MATRICES IN R
Download the updated scripts
demo_linear_trans.R
demo_eigenvectors.R
http://www.atmos.albany.edu/facstaff/timm/ATM315spring14/R/scripts/