Open In App

Sum All Elements in a Matrix using R

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)




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.




# 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.




# 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




# 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




# 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


Article Tags :