在 R 编程中返回一个上三角为 TRUE 值的矩阵 – upper.tri()函数
R 语言中的upper.tri()
函数用于返回一个逻辑值矩阵,其中上三角为 TRUE。
Syntax: upper.tri(x, diag)
Parameters:
x: Matrix object
diag: Boolean value to include diagonal
示例 1:
# R program to print the
# upper triangle of a matrix
# Creating a matrix
mat <- matrix(c(1:9), 3, 3, byrow = T)
# Calling upper.tri() Function
# Excluding diagonal elements
upper.tri(mat, diag = F)
输出:
[, 1] [, 2] [, 3]
[1, ] FALSE TRUE TRUE
[2, ] FALSE FALSE TRUE
[3, ] FALSE FALSE FALSE
示例 2:
# R program to print the
# upper triangle of a matrix
# Creating a matrix
mat <- matrix(c(1:9), 3, 3, byrow = T)
# Calling upper.tri() Function
# including diagonal elements
upper.tri(mat, diag = T)
输出:
[, 1] [, 2] [, 3]
[1, ] TRUE TRUE TRUE
[2, ] FALSE TRUE TRUE
[3, ] FALSE FALSE TRUE