How to Create an Exchange Matrix in R
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= diag (1, 4)
print (actual)
print (actual[4:1,])
|
Output:
Example 2:
In this example, we are creating 2*2 matrix and convert to an exchange matrix.
R
actual= diag (1, 2)
print (actual)
print (actual[2:1,])
|
Output:
Last Updated :
02 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...