Open In App

Sum of Antidiagonal of a Matrix in R

In linear algebra and matrix mathematics, an antidiagonal of a matrix refers to the set of elements that run diagonally from the bottom-left corner to the upper-right corner of the matrix. In R Programming Language Calculating the sum of the antidiagonal elements is a common operation in various data analysis and manipulation tasks using R. This article will provide you with a step-by-step explanation of how to compute the sum of the antidiagonal of a matrix in R.

Concepts Related to the Topic:

  1. Matrices in R: Understanding how to create and manipulate matrices using R is essential for this task.
  2. Indexing: Accessing specific elements in a matrix using indexing is crucial for extracting the antidiagonal elements.

Antidiagonal

The antidiagonal of a matrix consists of elements that are positioned along the opposite diagonal, running from the bottom-left to the upper-right of the matrix. and An antidiagonal matrix is a square matrix with all entries zero except those on the diagonal running from the lower left corner to the upper right corner (),



Steps Needed:

Step 1: Create a Matrix

You can start by creating a matrix in R. For this example, let’s create a simple 3×3 matrix.

Step 2: Compute the Antidiagonal Sum

To calculate the sum of the antidiagonal elements, you can use indexing and a loop to iterate through the matrix.



Step 3: Display the Result

You can then display the sum of the antidiagonal elements.

Example1 Sum of Antidiagonal of a Matrix in R




# Create a sample matrix
matrix_data <- matrix(1:9, nrow = 3)
 
# Initialize a variable to store the sum
anti_diag_sum <- 0
 
# Iterate through the matrix
for (i in 1:nrow(matrix_data)) {
  anti_diag_sum <- anti_diag_sum + matrix_data[i, ncol(matrix_data) - i + 1]
}
 
matrix_data
 
# Display the sum of the antidiagonal elements
cat("Sum of the antidiagonal elements:", anti_diag_sum, "\n")

Output:

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

Sum of the antidiagonal elements: 15 

Other Approaches

1) Reverse Rows and Sum Diagonal:




# Create the matrix
m <- matrix(c(2, 3, 1, 4, 2, 5, 1, 3, 7), 3)
 
print(m)
# 1) Reverse Rows and Sum Diagonal
result1 <- sum(diag(m[nrow(m):1, ]))
 
# Display the results
cat("Result 1: Sum of antidiagonal using reversed rows:", result1, "\n")

Output:
     [,1] [,2] [,3]
[1,]    2    4    1
[2,]    3    2    3
[3,]    1    5    7
Result 1: Sum of antidiagonal using reversed rows: 4 

First It creates a 3×3 matrix m by using the matrix function. The numbers provided in the c() function are filled into the matrix in column-major order, and the 3 as the second argument specifies that the matrix should have 3 columns. So, m will look like this.

2) Using Row and Col:




# Create the matrix
m <- matrix(c(2, 3, 1, 4, 2, 5, 1, 3, 7), 3)
m
 
# 2) Using Row and Col
result2 <- sum(m[c(row(m) + col(m) - nrow(m) == 1)])
 
 
# Display the results
cat("Result 2: Sum of antidiagonal using row and col:", result2, "\n")

Output:

     [,1] [,2] [,3]
[1,]    2    4    1
[2,]    3    2    3
[3,]    1    5    7
Result 2: Sum of antidiagonal using row and col: 4 

3) Using Sequence of Indexes:




# Create the matrix
m <- matrix(c(2, 3, 1, 4, 2, 5, 1, 3, 7), 3)
print(m)
 
# 3) Using Sequence of Indexes
n <- nrow(m)
result3 <- sum(m[seq(n, by = n-1, length = n)])
 
# Display the results
cat("Result 3: Sum of antidiagonal using sequence of indexes:", result3, "\n")

Output:

     [,1] [,2] [,3]
[1,]    2    4    1
[2,]    3    2    3
[3,]    1    5    7
Result 3: Sum of antidiagonal using sequence of indexes: 4 

4) Using Outer Function:




# Create the matrix
m <- matrix(c(2, 3, 1, 4, 2, 5, 1, 3, 7), 3)
print(m)
# 4) Using Outer Function
n <- nrow(m)
result4 <- sum(m[!c(outer(1:n, n:1, "-"))])
 
# Display the results
cat("Result 4: Sum of antidiagonal using outer function:", result4, "\n")

Output:

     [,1] [,2] [,3]
[1,]    2    4    1
[2,]    3    2    3
[3,]    1    5    7
Result 4: Sum of antidiagonal using outer function: 4 

These methods offer different ways to calculate the sum of antidiagonal elements in a matrix in R, allowing you to choose the one that best suits your specific needs or preferences.


Article Tags :