Skip to contents

Given the number of elements of a square grid, it returns the indices of the upper or lower triangular part, with or without diagonal, both in the main direction and in the mirrored direction. The item count starts from the bottom left and proceeds by incrementing x ("h") or y ("v") faster.

Usage

getTriang(ncell, part = "upper", mirror = FALSE, diag = TRUE, by = "h")

Arguments

ncell

integer indicating number of cells in grid

part

character indicating which part of grid to take. The possible choices are "upper" and "lower"

mirror

boolean indicating whether to take the main direction (FALSE) or the mirrored one (TRUE). Default FALSE

diag

boolean indicating wheater include (TRUE) diagonal or not (FALSE). Default TRUE

by

character indicating whether grid indices grow faster along x ("h") or y ("v")

Value

A vector of indices

Examples

df_grid <- expand.grid(x = 1:4, y = 1:4)
df_grid$index <- 1:16
df_grid
#>    x y index
#> 1  1 1     1
#> 2  2 1     2
#> 3  3 1     3
#> 4  4 1     4
#> 5  1 2     5
#> 6  2 2     6
#> 7  3 2     7
#> 8  4 2     8
#> 9  1 3     9
#> 10 2 3    10
#> 11 3 3    11
#> 12 4 3    12
#> 13 1 4    13
#> 14 2 4    14
#> 15 3 4    15
#> 16 4 4    16

# corresponding grid
# 13 14 15 16
#  9 10 11 12
#  5  6  7  8
#  1  2  3  4

getTriang(nrow(df_grid), part = "upper", mirror = TRUE, diag = TRUE, by = "h")
#>  [1]  1  5  6  9 10 11 13 14 15 16