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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

      [, 1]  [, 2] [, 3]
[1, ]  TRUE  TRUE TRUE
[2, ] FALSE  TRUE TRUE
[3, ] FALSE FALSE TRUE
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.