Skip to contents

Normalizes a numeric matrix dividing each row (if rows=TRUE) or column (if rows=FALSE) by their L2 norm. Thus, each row (or column) has unit norm.

Usage

cosnormX(X, rows = TRUE)

Arguments

X

Numeric matrix or data.frame of any size.

rows

If TRUE, the operation is done by row; otherwise, it is done by column. (Defaults: TRUE).

Value

Cosine-normalized X.

Examples

dat <- matrix(rnorm(50),ncol=5,nrow=10)
cosnormX(dat)
#>              [,1]       [,2]       [,3]        [,4]       [,5]
#>  [1,] -0.61469615  0.2462631 -0.7067614  0.16856036 -0.1832454
#>  [2,] -0.21311936 -0.3903413 -0.1876834  0.73240719  0.4801755
#>  [3,] -0.05260582 -0.5117497 -0.4416434  0.41254161  0.6083629
#>  [4,] -0.38294941  0.3647872 -0.5077878  0.60689923 -0.3067653
#>  [5,]  0.11059173  0.1863747 -0.2493527 -0.36870172 -0.8688591
#>  [6,]  0.51489811  0.7438531  0.2547167 -0.30000777 -0.1633316
#>  [7,] -0.02127708  0.5555483 -0.1724889 -0.46782746  0.6650552
#>  [8,] -0.64026961  0.5694931  0.4548650  0.04277097 -0.2387487
#>  [9,]  0.41222763  0.4353622 -0.1406495  0.54624196 -0.5677725
#> [10,] -0.71994417  0.4352655 -0.3096750 -0.10377169  0.4307635