GetRowCol: Retrieve matrix row and column indices

View source: R/GetRowCol.R

GetRowColR Documentation

Retrieve matrix row and column indices

Description

Here is a function that will find the row and column indices of a matrix that are associated with a vector index.

Usage

GetRowCol(Index, dim1, dim2)

Arguments

Index

an integer vector index

dim1

integer row dimension of the matrix from which the row and column indices are to be extracted

dim2

integer column dimension of the matrix from which the row and column indices are to be extracted

Details

Often when applying functions like the R function which.max(matrix) to a matrix, a vector index is returned when the coder would prefer to have a row and column indices. This function converts the vector index to row and column indices.

The function assumes that the elements of the matrix are filled by column (byrow = FALSE), which is the default R matrix behaviour.

Value

a numeric vector of length two with two integers indicating row and column respectively

Examples

GetRowCol(6, dim1 = 3, dim2 = 3) # should return c(3, 2)

ICvectorfields documentation built on March 18, 2022, 7:34 p.m.