If yes, here is a quick tutorial…
Let’s begin by understanding a Matrix. Well, a Matrix is an array of numbers, in a combination of rows and columns. The size of a matrix is defined by the number of rows and columns that it contains. A matrix with an m rows and n columns is called an m x n matrix or m-by-n matrix, while m and n are called its dimensions, denoted by [a_ij]
Entering a Matrix in R and understanding the tech jargon
Syntax: M=matrix(elements,dimension,arrangement)
Elements: are the entries to be entered in a matrix.
Dimension: defines the number of rows and columns in a matrix.
Arrangement: defines how do you want the enter the matrix(row wise or column wise).
Example: Enter the range of the values 1 to 6 in a 2×3 matrix
2×3 matrix will have 2 rows and three columns
M1=matrix(1:6,nrow=2,byrow=TRUE)
To enter the values in two rows we use nrow = 2 . To enter values in columns use ncol = “integer”.byrow =True tells R to enter the values row wise.
We can also enter the elements using c() function.
Note: R will take the values column wise in default. If we don’t specify “arrangement” condition and dimension then it will be a 6×1 matrix.
Entering an identity matrix in R
To enter an n dimension identity matrix in R we use diag (n) function.
Example: Let n =3
I-diag(3), this will enter a 3×3 matrix of dimension 3.
Operations in Matrices:
To add and subtract the matrices with same number of rows and columns. We use plus, minus and multiplication.
Let A and B be two matrices with same number of rows and columns.
M2=A+B
M3=A-B
M4=A*B
Note: You can see that these are vector wise addition, subtraction and multiplication.
Matrix multiplication: In order to multiply two matrices, A and B, the number of columns in A must equal the number of rows in B. Thus, if A is an m x n matrix and B is an r x s matrix, . n = r
Syntax: A% * %B
Note:
Solve a System of Equations
Syntax: solve (A,b)
A, a square numeric or complex matrix containing the coefficient of linear system and b a matrix which gives the right hand side of linear system.
Consider the linear equations x+y+z=6, 2y+5z=-4, 2x+5y-z=27.
If b is missing in the function solve (A,b) then b is taken to be identity matrix and result will be inverse of A.
Hope this was useful. If you have any questions, feel free to write in. And watch this space for many more useful guides to all things related to data and analytics and stats!
Suggested Read:
Faster Versions of R
Stringi Package in R
Fill in the details to know more
Important Artificial Intelligence Tools
October 31, 2022
Top 28 Data Analytics Tools For Data Analysts | UNext
September 27, 2022
May 5, 2022
Best Frameworks In Java You Should Know In 2021
May 5, 2021
Lean Management Tools: An Ultimate Overview For 2021
May 4, 2021
Talend ETL: An Interesting Guide In 4 Points
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile