将R中的矩阵转换为具有相应条目的上三角/下三角矩阵 [英] Convert a matrix in R into a upper triangular/lower triangular matrix with those corresponding entries

查看:1178
本文介绍了将R中的矩阵转换为具有相应条目的上三角/下三角矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对称矩阵,我想将其转换为R中的上三角/下三角矩阵.有没有办法做到这一点?

I have a symmetric matrix and I want to convert it into a upper triangular/lower triangular matrix in R. Is there a way of doing this ?

我无法使用upper.trilower.tri来执行此操作.使用这些为我提供了一个矩阵,其中的条目为TRUEFALSE.

I am not able to do this using upper.tri and lower.tri. Using these gives me a matrix with entries as either TRUE or FALSE.

推荐答案

要获取上三角矩阵:

mat <- matrix(1:9, 3, 3)
mat[lower.tri(mat)] <- 0

要删除对角线,请使用:

To remove diagonal, use:

mat[lower.tri(mat,diag=TRUE)] <- 0mat[!upper.tri(mat)] <- 0,如Karolis的评论所述.

mat[lower.tri(mat,diag=TRUE)] <- 0 or mat[!upper.tri(mat)] <- 0 as suggested in the comments by Karolis.

这篇关于将R中的矩阵转换为具有相应条目的上三角/下三角矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆