Is there an easy way to retrieve the upper(or lower) triangular matrix of a nonsymmetric matrix in R? For a symmetric matrix, this can be achieved using mat[upper.tri(mat)]
, but how about for a non-symmetric matrix? Here the definition of an upper triangular matrix is like this: if a cell having more than 1/2 of its part belongs to the upper right corner of the matrix delimited by the diagonal line, then this cell belongs to the upper triangular matrix (e.g., the red part in the figure).
Thanks.
Given any upper triangular matrix, you can find the value of the determinant simply by multiplying together all of the entries along the main diagonal of the matrix. This also tells you that, if you have a 0 anywhere along the main diagonal of an upper triangular matrix, that the determinant will be 0.
An upper triangular matrix is a matrix in which all the lower triangular elements are zero. That is, all the non-zero elements are on the main diagonal or in the upper triangle. That is, U is upper triangular if and only if: ∀aij∈U:i>j⟹aij=0.
In other words, a square matrix is upper triangular if all its entries below the main diagonal are zero. Example of a 2 × 2 upper triangular matrix: A square matrix with elements sij = 0 for j > i is termed lower triangular matrix.
U = triu( A ) returns the upper triangular portion of matrix A . U = triu( A , k ) returns the elements on and above the kth diagonal of A .
It's actually not so difficult:
mat[nrow(mat) * (2 * col(mat) - 1) / (2 * ncol(mat)) - row(mat) > -1/2]
# [1] 4 7 10 11 13 14 15
Imagine that your picture is the upper right quarter of the R^2 space. That is, the lower left corner corresponds to (0,0), and so on. Let nc
and nr
correspond do the number of columns and rows in your matrix, respectively. Also, let c
and r
correspond to the column and row of a particular cell.
It's easy to see that the equation of the diagonal line is y = nr - nr / nc * x
in the usual notation. What is left is computing the area corresponding to each (c,r) cell. The upper line of this cell is at the level y = nr - r + 1
and it goes from x = c - 1
to x = c
. Whenever this area is greater than 1/2, we include this cell to the answer. The matrix of those areas is given by
nrow(mat) * (2 * col(mat) - 1) / (2 * ncol(mat)) - row(mat) + 1
While the matrix is not square, there is still a lot of symmetry and, if the matrix were huge, you could exploit that and compute the areas for only 25%~ of the cells, but I assume that's not the case here.
Due to this symmetry, the lower triangular matrix is also very easy to get:
mat[nrow(mat) * (2 * col(mat) - 1) / (2 * ncol(mat)) - row(mat) < -1/2]
# [1] 1 2 3 5 6 9 12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With