R/linAndMatInd.R

Defines functions .linInd .matInd

.linInd <- function(matInd, #array indices
                    dimsizes) { #array containing size of array of interest in each dimension
  y <- matInd[1]
  if (length(dimsizes) > 1) {
    for (i in 2:length(dimsizes)) {
      y <- y + (matInd[i] - 1) * prod(dimsizes[seq_len(i - 1)])
    }
  }
  return(y)
}

.matInd <- function(linInd, #linear index
                    dimsizes) { #array containing size of array of interest in each dimension
  y <- matrix(0, nrow = length(dimsizes), ncol = 1)
  if (NROW(y) > 1) {
    for (i in seq(from = length(dimsizes), to = 2)) {
      y[i] <- ceiling(linInd / prod(dimsizes[seq_len(i - 1)]))
      linInd <- linInd - (y[i] - 1) * prod(dimsizes[seq_len(i - 1)])
    }
  }
  y[1] <- linInd
  return(y)
}

Try the CoreGx package in your browser

Any scripts or data that you put into this service are public.

CoreGx documentation built on Dec. 20, 2019, 1:08 a.m.