Sum All Elements in a Matrix using R
Last Updated :
28 Apr, 2025
Matrices in R Programming Language are the objects in which the elements are arranged in a 2-D rectangular layout. A matrix is a collection of elements of the same data type(numeric, character, or logical) arranged in a fixed number of rows and columns, as we very well know rows are represented horizontally and columns are represented vertically.
Creating Matrix
To create a matrix in R, we use the matrix() function. This function takes several arguments, including data(collection of elements), nrow, ncol, byrow.
Syntax: matrix(data, nrow, ncol, byrow=True)
- data :- It will describe the collection of elements that R will arrange into rows and columns of the matrix.
- nrow :- Describe the number of rows
- ncol :- Describes the number of columns
- byrow :- By default this field is set to "FALSE" means elements in the matrix are filled in column wise manner but if we explicitly set it to "TRUE" then elements in the matrix are filled in the row wise manner.
R
A= matrix(
c(10,20,30,40,50,60,70,80,90),
nrow=3,
ncol=3,
byrow=TRUE
)
cat("My 3*3 matrix :-\n")
print(A)
Output:
My 3*3 matrix :-
[,1] [,2] [,3]
[1,] 10 20 30
[2,] 40 50 60
[3,] 70 80 90
Accessing elements in different forms in the matrix
Suppose we have a 3*3 matrix i.e. 9 elements and now we need to access each element separately. Let's examine an example code to understand this.
R
# Define the matrix
A = matrix(
c(10, 20, 30, 40, 50, 60, 70, 80, 90),
nrow = 3,
ncol = 3,
byrow = TRUE
)
cat("My 3*3 matrix is:\n")
print(A)
# Access each element using nested loops
for (i in 1:nrow(A)) {
for (j in 1:ncol(A)) {
cat("Element at position (", i, ",", j, ") is:", A[i, j])
cat("\n")
}
}
Output:
My 3*3 matrix is:
[,1] [,2] [,3]
[1,] 10 20 30
[2,] 40 50 60
[3,] 70 80 90
Element at position ( 1 , 1 ) is: 10
Element at position ( 1 , 2 ) is: 20
Element at position ( 1 , 3 ) is: 30
Element at position ( 2 , 1 ) is: 40
Element at position ( 2 , 2 ) is: 50
Element at position ( 2 , 3 ) is: 60
Element at position ( 3 , 1 ) is: 70
Element at position ( 3 , 2 ) is: 80
Element at position ( 3 , 3 ) is: 90
Sum all elements in a matrix using R
Now, we are now well-equipped to calculate the sum of all elements in a matrix. We have above covered the process of matrix creation and explored various methods of accessing its elements.
R
# Define the matrix
A = matrix(
c(10, 20, 30, 40, 50, 60, 70, 80, 90),
nrow = 3,
ncol = 3,
byrow = TRUE
)
cat("MY 3*3 matrix is:\n")
print(A)
#used for storing the total sum
sum=0
# Access each element using nested loops
for (i in 1:nrow(A)) {
for (j in 1:ncol(A)) {
sum=sum+A[i, j] #adding each element
}
}
cat("Sum of all elements in the matrix are: ",sum)
Output:
MY 3*3 matrix is:
[,1] [,2] [,3]
[1,] 10 20 30
[2,] 40 50 60
[3,] 70 80 90
Sum of all elements in the matrix are: 450
Adding sum of the matrix element row by row
R
# Define a 3x3 matrix
matrix_data = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = TRUE)
matrix_data
# Initialize a variable to store the sum
total_sum = 0
# Loop through each row and calculate the sum
for (row in 1:nrow(matrix_data)) {
row_sum = sum(matrix_data[row, ])
cat("Sum of elements in row", row, ":", row_sum, "\n")
total_sum = total_sum + row_sum
}
# Display the total sum
cat("Total sum of all elements:", total_sum, "\n")
Output:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Sum of elements in row 1 : 6
Sum of elements in row 2 : 15
Sum of elements in row 3 : 24
Total sum of all elements: 45
Adding sum of the matrix element by column
R
# Define a 3x3 matrix
matrix_data = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow = TRUE)
matrix_data
# Initialize a variable to store the sum
total_sum = 0
# Loop through each column and calculate the sum
for (col in 1:ncol(matrix_data)) {
col_sum = sum(matrix_data[, col])
cat("Sum of elements in column", col, ":", col_sum, "\n")
total_sum = total_sum + col_sum
}
# Display the total sum
cat("Total sum of all elements:", total_sum, "\n")
Output:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Sum of elements in column 1 : 12
Sum of elements in column 2 : 15
Sum of elements in column 3 : 18
Total sum of all elements: 45
Similar Reads
Append a row to a matrix using R In this article, we will examine various methods to append a row into a matrix in the R Programming Language. What is a matrix?A matrix is a two-dimensional data set, which is a collection of rows and columns. It contains n rows and m columns. Inside the matrix, rows are arranged horizontally and co
4 min read
Split a Matrix into a List of its Rows using R In this article, we will discuss how to split a given matrix into a List of its rows using R Programming Language. A matrix in R is a two-dimensional data set with columns and rows that can hold homogeneous data. Splitting a matrix can be useful for various purposes in data manipulation and analysis
4 min read
Min, Max and Mean of Off-Diagonal Elements in a Matrix in R A matrix is a combination of elements stacked together in either row or column format. A table-like structure formed of similar data type elements is known as a matrix. A matrix has two diagonals, one of which is known as the main diagonal. The main diagonal elements are characterized by the proper
3 min read
How to Change Matrix Entries Using Conditional if in R Working with matrices is a fundamental task in R, especially when handling large datasets, mathematical computations, or creating models. Often, you'll need to modify elements within a matrix based on certain conditions. This article will guide you through changing matrix entries using conditional s
3 min read
Extract unique columns from a matrix using R A matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run vertically. In R programming Language, matrices are two-dimensional, homogeneous data structures. These are some examples of matrice
5 min read