R can treat matrices and arrays as 1D vectors, which can speed up indexing. This takes the row and column of the matrix and the number of rows (as R matrices are column-indexed) and returns the corresponding 1D index.

convert_indices_matrix_to_vector(i, j, n)

Arguments

i

the row index

j

the column index

n

the number of rows

Value

the 1D indices corresponding to a collapsed matrix

Examples

x <- matrix(runif(10*5),nrow=10,ncol=5)
x[3,3]
#> [1] 0.001932419
x[convert_indices_matrix_to_vector(3,3,10)]
#> [1] 0.001932419