Open In App

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

Last Updated : 16 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads