kIndex: Utilities for conversion between row-column indexing and...

View source: R/auxiliary.R

kIndexR Documentation

Utilities for conversion between row-column indexing and linear indexing of matrices

Description

Conversions between cbind(i, j) and k such that A[i, j] == A[k] for a matrix A. Either column or row ordering can be specified for the linear indexing, and also direct conversions between both types.

Usage

kIndex(i, j, nr, nc, byRows = FALSE)

ijIndex(k, nr, nc, byRows = FALSE)

kColToRow(k, nr, nc)

kRowToCol(k, nr, nc)

Arguments

i

row index.

j

column index.

nr

number of rows.

nc

number of columns.

byRows

whether to use row-ordering instead of the default column-ordering.

k

linear indexes for column-stacking or row-stacking ordering (if byRows = TRUE).

Value

Depending on the function:

  • kIndex: a vector of length nr * nc with the linear indexes for A.

  • ijIndex: a matrix of dimension c(length(k), 2) giving cbind(i, j).

  • kColToRow and kRowToCol: a vector of length nr * nc giving the permuting indexes to change the ordering of the linear indexes.

Examples

# Indexes of a 3 x 5 matrix
ij <- expand.grid(i = 1:3, j = 1:5)
kCols <- kIndex(i = ij[, 1], j = ij[, 2], nr = 3, nc = 5)
kRows <- kIndex(i = ij[, 1], j = ij[, 2], nr = 3, nc = 5, byRows = TRUE)

# Checks
ijIndex(kCols, nr = 3, nc = 5)
ij
ijIndex(kRows, nr = 3, nc = 5, byRows = TRUE)
ij

# Change column to row (and viceversa) ordering in the linear indexes
matrix(1:10, nr = 2, nc = 5)
kColToRow(1:10, nr = 2, nc = 5)
kRowToCol(kColToRow(1:10, nr = 2, nc = 5), nr = 2, nc = 5)

egarpor/sdetorus documentation built on March 4, 2024, 1:23 a.m.