Skip to content
Related Articles

Related Articles

Improve Article

Return a Matrix with Upper Triangle as TRUE values in R Programming – upper.tri() Function

  • Last Updated : 16 Jun, 2020

upper.tri() function in R Language is used to return a matrix of logical values with upper triangle as TRUE.

Syntax: upper.tri(x, diag)

Parameters:
x: Matrix object
diag: Boolean value to include diagonal

Example 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)

Output:

      [, 1]  [, 2]  [, 3]
[1, ] FALSE  TRUE  TRUE
[2, ] FALSE FALSE  TRUE
[3, ] FALSE FALSE FALSE

Example 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)

Output:

      [, 1]  [, 2] [, 3]
[1, ]  TRUE  TRUE TRUE
[2, ] FALSE  TRUE TRUE
[3, ] FALSE FALSE TRUE
My Personal Notes arrow_drop_up
Recommended Articles
Page :