Skip to contents

Replicates the subset functionality of a matrix in R.

Usage

get_elements(x, row_ind, col_ind)

Arguments

x

A matrix of dimensions M x N

row_ind

A unsigned int vec that contains the row indices within \([0,M-1]\).

col_ind

A unsigned int vec that contains the column indices within \([0,N-1]\).

Value

A vec with each element listed according to index specification.

Author

JJB

Examples

# Generate a Matrix
m = matrix(1:12, nrow = 4)

# Select Non-connect regions
row_index = c(1, 2, 1)
col_index = c(2, 2, 3)

# Subset in R
m[cbind(row_index, col_index)]
#> [1] 5 6 9

# Subset with Armadillo
get_elements(m, row_index - 1, col_index - 1)
#>      [,1]
#> [1,]    5
#> [2,]    6
#> [3,]    9