Open In App

How to Create an Exchange Matrix in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to create an exchange matrix in R Programming Language.

The exchange matrix is a square matrix with ones on the anti-diagonal and zeros on all other elements. We can say that the exchange matrix is a combination of the identity matrix and the anti-diagonal matrix. We can call it a reversal matrix.

Matrix:    Exchange matrix:
1 0 0      0 0 1 
0 1 0      0 1 0
0 1 1      1 1 0

In R, we can create an exchange matrix by using the diag() methods

Syntax: diag(1, dimensions)[dimensions:1,]

where, diag is used to create the diagonal elements in a matrix and dimensions represent size of the matrix.

Example 1:

In this example, we are creating 4*4 matrix and convert to exchange matrix.

R




# actual 4*4 matrix
actual=diag(1, 4)
  
# display
print(actual)
  
# create 4*4 exchange matrix 
# from actual matrix
print(actual[4:1,])


Output:

 

Example 2:

In this example, we are creating 2*2 matrix and convert to an exchange matrix.

R




# actual 2*2 matrix
actual=diag(1, 2)
  
# display
print(actual)
  
# create 2*2 exchange matrix
# from actual matrix
print(actual[2:1,])


Output:

 


Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads