R Matrix
R Matrix
Home Python Java JavaScript HTML SQL PHP C#
← prev next →
R Matrix
In the R matrix, elements are arranged in a fixed number of rows and columns. The matrix
elements are the real numbers. In R, we use matrix function, which can easily reproduce the
memory representation of the matrix. In the R matrix, all the elements must share a
common basic type.
Example
ADVERTISEMENT
matrix1<-matrix(c(11, 13, 15, 12, 14, 16),nrow =2, ncol =3, byrow = TRUE)
matrix1
Output
History of matrices in R
ADVERTISEMENT
The word "Matrix" is the Latin word for womb which means a place where something is
formed or produced. Two authors of historical importance have used the word "Matrix" for
unusual ways. They proposed this axiom as a means to reduce any function to one of the
lower types so that at the "bottom" (0order) the function is identical to its extension.
ADVERTISEMENT
Any possible function other than a matrix from the matrix holds true with the help of the
process of generalization. It will be true only when the proposition (which asserts function
in question) is true. It will hold true for all or one of the value of argument only when the
other argument is undetermined.
data
The first argument in matrix function is data. It is the input vector which is the data
elements of the matrix.
nrow
The second argument is the number of rows which we want to create in the matrix.
ncol
The third argument is the number of columns which we want to create in the matrix.
byrow
The byrow parameter is a logical clue. If its value is true, then the input vector elements are
arranged by row.
dim_name
The dim_name parameter is the name assigned to the rows and columns.
Let's see an example to understand how matrix function is used to create a matrix and
arrange the elements sequentially by row or column.
Example
Output
Like C and C++, we can easily access the elements of our matrix by using the index of the
element. There are three ways to access the elements from the matrix.
1. We can access the element which presents on nth row and mth column.
2. We can access all the elements of the matrix which are present on the nth row.
3. We can also access all the elements of the matrix which are present on the mth column.
Let see an example to understand how elements are accessed from the matrix present on
nth row mth column, nth row, or mth column.
Example
Output
[1] 12
col1 col2 col3
11 12 13
matrix[n, m]<-y
Here, n and m are the rows and columns of the element, respectively. And, y is the value
which we assign to modify our matrix.
Example
Output
Example 1
Output
Example 2
Output
col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 12 13
row4 14 15 16
Example 1
#Adding row
rbind(R,c(17,18,19))
#Adding column
cbind(R,c(17,18,19,20))
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 5 8 11 14 6 9 12 15 7 10 13 16
Matrix operations
In R, we can perform the mathematical operations on a matrix such as addition,
subtraction, multiplication, etc. For performing the mathematical operation on the matrix,
it is required that both the matrix should have the same dimensions.
Let see an example to understand how mathematical operations are performed on the
matrix.
Example 1
#Addition
sum<-R+S
print(sum)
#Subtraction
sub<-R-S
print(sub)
#Multiplication
mul<-R*S
print(mul)
#Multiplication by constant
mul1<-R*12
print(mul1)
#Division
div<-R/S
print(div)
Output
Applications of matrix
1. In geology, Matrices takes surveys and plot graphs, statistics, and used to study in different
fields.
2. Matrix is the representation method which helps in plotting common survey things.
3. In robotics and automation, Matrices have the topmost elements for the robot movements.
4. Matrices are mainly used in calculating the gross domestic products in Economics, and it also
helps in calculating the capability of goods and products.
5. In computer-based application, matrices play a crucial role in the creation of realistic seeming
motion.
(a). In the given code, the rectangled line of code is mentioned wrong. (In the How to
create a matrix in R? section)
Here is the corrected line of code; replace it with the above line of code:
(b). The line of code is mentioned wrong in the given image: (In the Accessing matrix
elements in R section).
Replace the above line of code with the following corrected code:
(c). The line of code is mentioned wrong in the given image: (In the Assign a single
element section).
Replace the above line of code with the following corrected code:
col_names = c("col1", "col2", "col3")
(d). The line of code is mentioned wrong in the given image: (In the Use of Relational
Operator section).
Replace the above line of code with the following corrected code:
(e). The line of code is mentioned wrong in the given image: (In the Use of Relational
Operator section).
Replace the above line of code with the following corrected code:
(f). The line of code is mentioned wrong in the given image: (In the Addition of Rows and
Columns section).
Replace the above line of code with the following corrected code:
← prev next →