Scale matrix

scale_matrix(X, X_mean = NULL, X_sd = NULL)

Arguments

X

A matrix

X_mean

Mean of each column

X_sd

Standard deviation of each column

Value

A list containing the scaled matrix, mean of each column, and standard deviation of each column

Examples


X <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
(X_scaled <- misc::scale_matrix(X))
#> $X
#>            [,1]       [,2]       [,3]
#> [1,] -0.7071068 -0.7071068 -0.7071068
#> [2,]  0.7071068  0.7071068  0.7071068
#> 
#> $X_mean
#> [1] 1.5 3.5 5.5
#> 
#> $X_sd
#> [1] 0.7071068 0.7071068 0.7071068
#> 
(X_scaled <- misc::scale_matrix(X, X_mean = colMeans(X), X_sd = apply(X, 2, stats::sd)))
#> $X
#>            [,1]       [,2]       [,3]
#> [1,] -0.7071068 -0.7071068 -0.7071068
#> [2,]  0.7071068  0.7071068  0.7071068
#> 
#> $X_mean
#> [1] 1.5 3.5 5.5
#> 
#> $X_sd
#> [1] 0.7071068 0.7071068 0.7071068
#> 
print(colMeans(X_scaled$X))
#> [1] 0 0 0
print(apply(X_scaled$X, 2, stats::sd))
#> [1] 1 1 1