Successfully reported this slideshow.

# Vectormaths and Matrix in R.pptx

Upcoming SlideShare
Decision Tree.pptx
×

# Vectormaths and Matrix in R.pptx

R, Python

R, Python

## More Related Content

### Vectormaths and Matrix in R.pptx

1. 1. Vector Maths Arithmetic operations of vectors are performed member-by-member, i.e., member wise. For example, suppose we have two vectors a and b. > a = c(1, 3, 5, 7) > b = c(1, 2, 4, 8) Then, if we multiply a by 5, we would get a vector with each of its members multiplied by 5. > 5 * a [1] 5 15 25 35 And if we add a and b together, the sum would be a vector whose members are the sum of the corresponding members from a and b. > a + b [1] 2 5 9 15 Similarly for subtraction, multiplication and division, we get new vectors via memberwise operations. > a - b [1] 0 1 1 -1 > a * b [1] 1 6 20 56 > a / b [1] 1.000 1.500 1.250 0.875
2. 2. Recycling Rule • If two vectors are of unequal length, the shorter one will be recycled in order to match the longer vector. For example, the following vectors u and v have different lengths, and their sum is computed by recycling values of the shorter vector u. • > u = c(10, 20, 30) > v = c(1, 2, 3, 4, 5, 6, 7, 8, 9) > u + v [1] 11 22 33 14 25 36 17 28 39
3. 3. Vector Index • Vector Index • We retrieve values in a vector by declaring an index inside a single square bracket "[]" operator. • For example, the following shows how to retrieve a vector member. Since the vector index is 1-based, we use the index position 3 for retrieving the third member. • > s = c("aa", "bb", "cc", "dd", "ee") > s[3] [1] "cc"
4. 4. Negative Index • If the index is negative, it would strip the member whose position has the same absolute value as the negative index. • For example, the following creates a vector slice with the third member removed. • > s[-3] [1] "aa" "bb" "dd" "ee" • Out-of-Range Index • If an index is out-of-range, a missing value will be reported via the symbol NA. • > s[10] [1] NA
5. 5. Matrix • A matrix is a collection of data elements arranged in a two-dimensional rectangular layout. The following is an example of a matrix with 2 rows and 3 columns. • We reproduce a memory representation of the matrix in R with the matrix function. The data elements must be of the same basic type.
6. 6. • > A = matrix( + c(2, 4, 3, 1, 5, 7), # the data elements + nrow=2, # number of rows + ncol=3, # number of columns + byrow = T • A # print the matrix [,1] [,2] [,3] [1,] 2 4 3 [2,] 1 5 7RUE) # fill matrix by rows
7. 7. • An element at the mth row, nth column of A can be accessed by the expression A[m, n]. • > A[2, 3] # element at 2nd row, 3rd column [1] 7 • The entire mth row A can be extracted as A[m, ]. • > A[2, ] # the 2nd row [1] 1 5 7 • Similarly, the entire nth column A can be extracted as A[ ,n]. • > A[ ,3] # the 3rd column [1] 3 7 • We can also extract more than one rows or columns at a time. • > A[ ,c(1,3)] # the 1st and 3rd columns [,1] [,2] [1,] 2 3 [2,] 1 7
8. 8. Matrix Addition & Subtraction • # Create two 2x3 matrices. • matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2) print(matrix1) • matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2) print(matrix2) • # Add the matrices. • result <- matrix1 + matrix2 • cat("Result of addition","n") • print(result) • # Subtract the matrices • result <- matrix1 - matrix2 • cat("Result of subtraction","n") print(result)
9. 9. Matrix Multiplication & Division • # Create two 2x3 matrices. • matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2) print(matrix1) • matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2) print(matrix2 • # Multiply the matrices. • result <- matrix1 * matrix2 • cat("Result of multiplication","n") print(result))
10. 10. • # Divide the matrices • result <- matrix1 / matrix2 • cat("Result of division","n") • print(result)
11. 11. Matrix Access • Create a Matrix • x<-1:12 • Print(x) • Mat<-matrix(x,3,4) • Access third row of an existing matrix • Mat[3,] • Second column • Mat[,2] • Access second and third column • Mat[,2,3]
12. 12. Contour plot • Contour plots are used to show 3- dimensional data on a 2-dimensional surface. The most common example of a contour plot is a topographical map, which shows latitude and longitude on the y and x axis, and elevation overlaid with contours. Colours can also be used on the map surface to further highlight the contours
13. 13. • Create a matrix, mat which is 9 rows and 9 columns with all values are 1 • Mat<-matrix(1,9,9) • Print(mat) • Replace a value of 3rd row 3rd column • Mat[3,3]<-0 • Contour(mat) • Create a 3D perspective plot with persp(). It provides 3D wireframe plot • Persp(mat) • R includes some pre defined data sets • Volcano, which is 3D map dormant of New Zealand • Contour(volcano) • Heat map • Image(volcano)