colDivide: Row-wise division of a matrix by a column vector

View source: R/CodeAndRoll2.R

colDivideR Documentation

Row-wise division of a matrix by a column vector

Description

Each element of the matrix is divided by the corresponding element of the vector that matches the column of the matrix element. This is typically used to normalize data, for example, to scale values in each row by certain factors like totals or means. Soruce https://stackoverflow.com/questions/20596433/how-to-divide-each-row-of-a-matrix-by-elements-of-a-vector-in-r.

Usage

colDivide(mat, vec = colSums(mat))

Arguments

mat

A numeric matrix where each row represents a distribution to be divided.

vec

A numeric vector whose elements are the divisors for each column of the matrix. The length of the vector must match the number of columns in the matrix. If not supplied, the default is to use the column sums of the matrix as divisors.

Value

A matrix with the same dimensions as the input where each element in the original matrix has been divided by the corresponding element in the vector.


vertesy/CodeAndRoll2 documentation built on Nov. 20, 2024, 5:23 a.m.