pkern_vec2mat: Invert column-vectorization indices

View source: R/pkern_index.R

pkern_vec2matR Documentation

Invert column-vectorization indices

Description

Inverts the function pkern_mat2vec, returning matrix row and column numbers i, j, given the column-vectorized index k and matrix dimensions gdim.

Usage

pkern_vec2mat(k, gdim, out = "matrix")

Arguments

k

a vector of positive integers, the vector indices to look up

gdim

integer (or vector with first element equal to) the number of rows in the matrix

out

either 'matrix' or 'list'

Details

Output indices are returned in a matrix with columns 'i', 'j' and rows in same order as the input k. When out='list' list of vectors 'i' and 'j' (with entries in the same order) is returned instead.

The entries of k can be any permutation with replacement from seq(prod(gdim))

Value

a two column matrix of integers (row and column numbers) with length(k) rows

Examples


# show how elements are ordered in `base::matrix`
gdim = c(5, 6)
matrix_order = matrix(1:prod(gdim), gdim)
print(matrix_order)

# identify the row and column numbers for specific entry, or several
pkern_vec2mat(2, gdim)
pkern_vec2mat(c(2, 10, 5), gdim)
pkern_vec2mat(c(2, 10, 5), gdim, out='list')


deankoch/pkern documentation built on Oct. 26, 2023, 8:54 p.m.