SlideShare a Scribd company logo
1 of 16
Download to read offline
Working With Matrices in R
Dr. T. N. Kavitha
Assistant Professor of Mathematics
SCSVMV
To create a vector in R
• > vec <- c( 1.9, -2.8, 5.6, 0, 3.4, -4.2 )
• Then the vector looks like:
• > vec
• [1] 1.9 -2.8 5.6 0.0 3.4 -4.2
2
Dr T N Kavitha
To make a sequence of integers
• > vec <- -2:5
• Then the vector looks like:
• > vec
• [1] -2 -1 0 1 2 3 4 5
• > vec <- 5:-2
• Then we get:
• > vec
• [1] 5 4 3 2 1 0 -1 -2
3
Dr T N Kavitha
To create the sequence function
• We have to give a starting value, and ending value, and an
increment value, e.g.,
• > vec <- seq( -3.1, 2.2, by=0.1 )
• Then we get:
• > vec
• [1] -3.1 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7
• [16] -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2
• [31] -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3
• [46] 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2
Note that R wraps the vector around to four rows, and at the beginning of each
row gives you the position in the vector of the first element of the row. We can
run a sequence backward, from a larger value to a smaller value, but we must
use a negative increment value.
4
Dr T N Kavitha
To find the replicate function to a get a
vector whose elements are all the same
• > vec <- rep( 1, 8 )
• Then we get:
• > vec
• [1] 1 1 1 1 1 1 1 1
That is, we get 1 replicated 8 times.
• two or more vectors to make a larger vector, e.g.,
• > vec <- c( rep(2,4), 1:3, c(8,1,9) )
• Then we get
• > vec
• [1] 2 2 2 2 1 2 3 8 1 9
5
Dr T N Kavitha
To know how many elements are in the
vector
• > length(vec)
If we want to reference an element in the vector,
we use square brackets, e.g.,
• > vec[5]
This will give us the fifth element of the vector vec.
• To reference a consecutive subset of the vector,
we use the colon operator within the square
brackets, e.g.,
• > vec[2:7]
• This will give us elements two through seven of
the vector vec.
6
Dr T N Kavitha
To make a 2 x 3 matrix named M consisting
of the integers 1 through 6
• > M <- matrix( 1:6, nrow=2 )
• or this:
• > M <- matrix( 1:6, ncol=3 )
• We get:
• > M
• [,1] [,2] [,3]
• [1,] 1 3 5
• [2,] 2 4 6
• Note that R places the row numbers on the left
and the column numbers on top. Also note that R
filled in the matrix column-by-column.
7
Dr T N Kavitha
If we prefer to fill in the matrix row-by-row,
we must activate the byrow setting, e.g.,
• > M <- matrix( 1:6, ncol=3 )
• Then we get:
• > M
• [,1] [,2] [,3]
• [1,] 1 2 3
• [2,] 4 5 6
In place of the vector 1:6 you would place
any vector containing the desired elements of
the matrix.
8
Dr T N Kavitha
To obtain the transpose of a matrix
• > t(M)
• This gives us the transpose of the matrix
M created above:
• > t(M)
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
9
Dr T N Kavitha
To add or subtract two matrices (or vectors)
which have the same number of rows and
columns
• we use the plus and minus symbols, e.g.,
• > A + B
• > A - B
• To multiply two matrices with compatible dimensions
• > A %*% B
If we just use the multiplication operator *, R will multiply
the corresponding elements of the two matrices,
provided they have the same dimensions.
• To multiply two vectors to get their scalar (inner)
product, we use the same operator, e.g.,
• > a %*% b
• R will transpose a for us rather than giving us an error
message.
10
Dr T N Kavitha
To create the identity matrix for a desired
dimension
• > I <- diag(5)
• This gives us the 5 x 5 identity matrix:
• > I
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
11
Dr T N Kavitha
To find the determinant of a square matrix N
• To find the determinant of a square matrix
N, use the determinant function, e.g.,
• > det( N )
12
Dr T N Kavitha
To obtain the inverse N-1 of an invertible
square matrix N
• > solve( N )
• If the matrix is singular (not invertible), or almost
singular, we get an error message.
• A <- as.matrix(cbind(c(3,0),c(1,2)))
• A
• A1 <- matrix.inverse(A)
• A1
• solve(A)
• A %*% A1
• [,1] [,2]
• ## [1,] 1 0
## [2,] 0 1
13
Dr T N Kavitha
>mp <-
matrix(c(0,1/4,1/4,3/4,0,1/4,1/4,3/4,1/2),3,
3,byrow=T)
> mp
>eigen(mp)
> $vectors
Øsolve(eigen(mp)$vectors)
14
Dr T N Kavitha
TUTORIAL
• https://www.geeksforgeeks.org/find-
eigenvalues-and-eigenvectors-of-a-matrix-
in-r-programming-eigen-function/
15
Dr T N Kavitha
16
Dr T N Kavitha

More Related Content

What's hot

Matlab programming project
Matlab programming projectMatlab programming project
Matlab programming project
Assignmentpedia
 
Matlab HTI summer training course Lecture3
Matlab HTI summer training course Lecture3Matlab HTI summer training course Lecture3
Matlab HTI summer training course Lecture3
Mohamed Awni
 
Basics of programming in matlab
Basics of programming in matlabBasics of programming in matlab
Basics of programming in matlab
AKANKSHA GUPTA
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
Damian T. Gordon
 

What's hot (20)

Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Matlab-Data types and operators
Matlab-Data types and operatorsMatlab-Data types and operators
Matlab-Data types and operators
 
Matlab Introduction
Matlab IntroductionMatlab Introduction
Matlab Introduction
 
Matlab programming project
Matlab programming projectMatlab programming project
Matlab programming project
 
Matlab HTI summer training course Lecture3
Matlab HTI summer training course Lecture3Matlab HTI summer training course Lecture3
Matlab HTI summer training course Lecture3
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Simulink
SimulinkSimulink
Simulink
 
Basics of programming in matlab
Basics of programming in matlabBasics of programming in matlab
Basics of programming in matlab
 
Matlab for diploma students(1)
Matlab for diploma students(1)Matlab for diploma students(1)
Matlab for diploma students(1)
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & Scientists
 
Matlab solved problems
Matlab solved problemsMatlab solved problems
Matlab solved problems
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Matlab from Beginner to Expert
Matlab from Beginner to ExpertMatlab from Beginner to Expert
Matlab from Beginner to Expert
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlab
 
Basic matlab and matrix
Basic matlab and matrixBasic matlab and matrix
Basic matlab and matrix
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
MATLAB - The Need to Know Basics
MATLAB - The Need to Know BasicsMATLAB - The Need to Know Basics
MATLAB - The Need to Know Basics
 
Forelasning4
Forelasning4Forelasning4
Forelasning4
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 

Similar to working with matrices in r

Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modified
Inbok Lee
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 

Similar to working with matrices in r (20)

Vectormaths and Matrix in R.pptx
Vectormaths and Matrix in R.pptxVectormaths and Matrix in R.pptx
Vectormaths and Matrix in R.pptx
 
Matrix algebra in_r
Matrix algebra in_rMatrix algebra in_r
Matrix algebra in_r
 
Basics of Matlab for students and faculty
Basics of Matlab for students and facultyBasics of Matlab for students and faculty
Basics of Matlab for students and faculty
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Vectors.pptx
Vectors.pptxVectors.pptx
Vectors.pptx
 
Matlab Tutorial.ppt
Matlab Tutorial.pptMatlab Tutorial.ppt
Matlab Tutorial.ppt
 
Matlab ch1 (3)
Matlab ch1 (3)Matlab ch1 (3)
Matlab ch1 (3)
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modified
 
Lec2
Lec2Lec2
Lec2
 
Vector in R
Vector in RVector in R
Vector in R
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Ch07 linearspacealignment
Ch07 linearspacealignmentCh07 linearspacealignment
Ch07 linearspacealignment
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Recently uploaded (20)

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 

working with matrices in r

  • 1. Working With Matrices in R Dr. T. N. Kavitha Assistant Professor of Mathematics SCSVMV
  • 2. To create a vector in R • > vec <- c( 1.9, -2.8, 5.6, 0, 3.4, -4.2 ) • Then the vector looks like: • > vec • [1] 1.9 -2.8 5.6 0.0 3.4 -4.2 2 Dr T N Kavitha
  • 3. To make a sequence of integers • > vec <- -2:5 • Then the vector looks like: • > vec • [1] -2 -1 0 1 2 3 4 5 • > vec <- 5:-2 • Then we get: • > vec • [1] 5 4 3 2 1 0 -1 -2 3 Dr T N Kavitha
  • 4. To create the sequence function • We have to give a starting value, and ending value, and an increment value, e.g., • > vec <- seq( -3.1, 2.2, by=0.1 ) • Then we get: • > vec • [1] -3.1 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 • [16] -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 • [31] -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 • [46] 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 Note that R wraps the vector around to four rows, and at the beginning of each row gives you the position in the vector of the first element of the row. We can run a sequence backward, from a larger value to a smaller value, but we must use a negative increment value. 4 Dr T N Kavitha
  • 5. To find the replicate function to a get a vector whose elements are all the same • > vec <- rep( 1, 8 ) • Then we get: • > vec • [1] 1 1 1 1 1 1 1 1 That is, we get 1 replicated 8 times. • two or more vectors to make a larger vector, e.g., • > vec <- c( rep(2,4), 1:3, c(8,1,9) ) • Then we get • > vec • [1] 2 2 2 2 1 2 3 8 1 9 5 Dr T N Kavitha
  • 6. To know how many elements are in the vector • > length(vec) If we want to reference an element in the vector, we use square brackets, e.g., • > vec[5] This will give us the fifth element of the vector vec. • To reference a consecutive subset of the vector, we use the colon operator within the square brackets, e.g., • > vec[2:7] • This will give us elements two through seven of the vector vec. 6 Dr T N Kavitha
  • 7. To make a 2 x 3 matrix named M consisting of the integers 1 through 6 • > M <- matrix( 1:6, nrow=2 ) • or this: • > M <- matrix( 1:6, ncol=3 ) • We get: • > M • [,1] [,2] [,3] • [1,] 1 3 5 • [2,] 2 4 6 • Note that R places the row numbers on the left and the column numbers on top. Also note that R filled in the matrix column-by-column. 7 Dr T N Kavitha
  • 8. If we prefer to fill in the matrix row-by-row, we must activate the byrow setting, e.g., • > M <- matrix( 1:6, ncol=3 ) • Then we get: • > M • [,1] [,2] [,3] • [1,] 1 2 3 • [2,] 4 5 6 In place of the vector 1:6 you would place any vector containing the desired elements of the matrix. 8 Dr T N Kavitha
  • 9. To obtain the transpose of a matrix • > t(M) • This gives us the transpose of the matrix M created above: • > t(M) [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 9 Dr T N Kavitha
  • 10. To add or subtract two matrices (or vectors) which have the same number of rows and columns • we use the plus and minus symbols, e.g., • > A + B • > A - B • To multiply two matrices with compatible dimensions • > A %*% B If we just use the multiplication operator *, R will multiply the corresponding elements of the two matrices, provided they have the same dimensions. • To multiply two vectors to get their scalar (inner) product, we use the same operator, e.g., • > a %*% b • R will transpose a for us rather than giving us an error message. 10 Dr T N Kavitha
  • 11. To create the identity matrix for a desired dimension • > I <- diag(5) • This gives us the 5 x 5 identity matrix: • > I [,1] [,2] [,3] [,4] [,5] [1,] 1 0 0 0 0 [2,] 0 1 0 0 0 [3,] 0 0 1 0 0 [4,] 0 0 0 1 0 [5,] 0 0 0 0 1 11 Dr T N Kavitha
  • 12. To find the determinant of a square matrix N • To find the determinant of a square matrix N, use the determinant function, e.g., • > det( N ) 12 Dr T N Kavitha
  • 13. To obtain the inverse N-1 of an invertible square matrix N • > solve( N ) • If the matrix is singular (not invertible), or almost singular, we get an error message. • A <- as.matrix(cbind(c(3,0),c(1,2))) • A • A1 <- matrix.inverse(A) • A1 • solve(A) • A %*% A1 • [,1] [,2] • ## [1,] 1 0 ## [2,] 0 1 13 Dr T N Kavitha
  • 14. >mp <- matrix(c(0,1/4,1/4,3/4,0,1/4,1/4,3/4,1/2),3, 3,byrow=T) > mp >eigen(mp) > $vectors Øsolve(eigen(mp)$vectors) 14 Dr T N Kavitha
  • 16. 16 Dr T N Kavitha