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)
the row index
the column index
the number of rows
the 1D indices corresponding to a collapsed matrix