Transform the Scaled Matrix to its Original Form in R Programming - Using Matrix Computations
Last Updated :
18 Jan, 2022
In
R programming, a matrix can be scaled and centered using
scale()
function. But, there is no in-built function to transform the scaled matrix back to the original matrix. In this article, we'll learn to transform the scaled matrix back to the original matrix using some simple computations.
Using Matrix Attributes
Attributes of the scaled matrix can be used to unscale the matrix back to its original matrix.
R
# Create matrix
mt <- matrix(c(1:20), ncol = 5)
# Print original matrix
cat("Original matrix:\n")
print(mt)
# Scale the matrix
scaled.mt <- scale(mt)
# Print Scaled matrix
cat("Scaled matrix:\n")
print(scaled.mt)
# Unscale the matrix
unscaled.mt <- t(apply(scaled.mt, 1,
function(r) r * attr(scaled.mt, 'scaled:scale') +
attr(scaled.mt, 'scaled:center')))
# Print Unscaled matrix
cat("Unscaled matrix:\n")
print(unscaled.mt)
Output:
Original matrix:
[, 1] [, 2] [, 3] [, 4] [, 5]
[1, ] 1 5 9 13 17
[2, ] 2 6 10 14 18
[3, ] 3 7 11 15 19
[4, ] 4 8 12 16 20
Scaled matrix:
[, 1] [, 2] [, 3] [, 4] [, 5]
[1, ] -1.1618950 -1.1618950 -1.1618950 -1.1618950 -1.1618950
[2, ] -0.3872983 -0.3872983 -0.3872983 -0.3872983 -0.3872983
[3, ] 0.3872983 0.3872983 0.3872983 0.3872983 0.3872983
[4, ] 1.1618950 1.1618950 1.1618950 1.1618950 1.1618950
attr(, "scaled:center")
[1] 2.5 6.5 10.5 14.5 18.5
attr(, "scaled:scale")
[1] 1.290994 1.290994 1.290994 1.290994 1.290994
Unscaled matrix:
[, 1] [, 2] [, 3] [, 4] [, 5]
[1, ] 1 5 9 13 17
[2, ] 2 6 10 14 18
[3, ] 3 7 11 15 19
[4, ] 4 8 12 16 20
Using Mean and Standard Deviation
In this example, we'll unscale the scaled matrix using mean and standard deviation of the original matrix.
R
# Create matrix
a <- rnorm(5, 5, 2)
b <- rnorm(5, 7, 5)
mt <- cbind(a, b)
# get center and scaling values
m <- apply(df, 2, mean)
s <- apply(df, 2, sd)
# Print original matrix
cat("Original matrix:\n")
print(mt)
# Scale the matrix
scaled.mt <- scale(mt, center = m, scale = s)
# Print Scaled matrix
cat("Scaled matrix:\n")
print(scaled.mt)
# Unscale the matrix
unscaled.mt <- t((t(scaled.mt) * s) + m)
# Print Unscaled matrix
cat("Unscaled matrix:\n")
print(unscaled.mt)
Output:
Original matrix:
a b
[1, ] 5.552301 1.9865159
[2, ] 3.936486 17.5327829
[3, ] 5.379720 15.0981877
[4, ] 3.546333 0.5230305
[5, ] 5.043194 2.0930855
Scaled matrix:
a b
[1, ] 0.01543556 -2.062174
[2, ] -0.69057233 5.252016
[3, ] -0.05997146 4.106591
[4, ] -0.86104456 -2.750713
[5, ] -0.20701146 -2.012036
attr(, "scaled:center")
a b
5.516974 6.369655
attr(, "scaled:scale")
a b
2.288663 2.125494
Unscaled matrix:
a b
[1, ] 5.552301 1.9865159
[2, ] 3.936486 17.5327829
[3, ] 5.379720 15.0981877
[4, ] 3.546333 0.5230305
[5, ] 5.043194 2.0930855
attr(, "scaled:center")
a b
5.516974 6.369655
attr(, "scaled:scale")
a b
2.288663 2.125494
Similar Reads
Get Transpose of a Matrix or Data Frame in R Programming - t() Function t() function in R Language is used to calculate transpose of a matrix or Data Frame. Syntax: t(x) Parameters: x: matrix or data frame Example 1: Python3 # R program to illustrate # t function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling t() function t(BOD) Output: Time demand 1 1 8.3
1 min read
Calculate the cross-product of the Transpose of a Matrix in R Programming - tcrossprod() Function tcrossprod() function in R Language is used to return the cross-product of the transpose of the specified matrix. Syntax: tcrossprod(x) Parameters: x: numeric matrix Example 1: Python3 # R program to illustrate # tcrossprod function # Initializing a matrix with # 2 rows and 2 columns x <- matrix(
1 min read
Scale the Columns of a Matrix in R Programming - scale() Function The scale() function in R is used to center and/or scale the columns of a numeric matrix. It helps standardize data by transforming it to have a mean of 0 and standard deviation of 1 .Syntax scale(x, center = TRUE, scale = TRUE) Parameters: x: represents numeric matrix .center: represents either log
2 min read
Convert an Object into a Matrix in R Programming - as.matrix() Function The as.matrix() function within R converts objects of various classes into a matrix. This can be helpful to work with structures of various data that can be converted into the matrix structure so that it becomes easier to analyze.Syntax: as.matrix(x)Parameters: x: Object to be convertedExample 1: Co
2 min read
How to Convert Sparse Matrix to Dense Matrix in R? R is a programming language used for performing statistical computation and graphical purposes. It is currently supported and backed by the R Core Team and the R Foundation for Statistical Computing. For data analysis and the creation of statistical software, R Programming Language is used by statis
2 min read
Getting a Matrix of number of rows in R Programming - row() Function In this article, we will discuss how to find the number of rows and columns in a matrix in R Programming Language. nrow() Function in Rnrow() function in R Language is used to get the row number of a matrix. Syntax: row(x, as.factor=FALSE) Parameters:x: matrixas.factor: a logical value indicating w
2 min read
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: Python3 1== # R program to convert a data frame # into a nu
2 min read
Get or Set Dimensions of a Matrix in R Programming - dim() Function The dim() function in R is used to get or set the dimensions of an object. This function is particularly useful when working with matrices, arrays, and data frames. Below, we will discuss how to use dim() to both retrieve and set dimensions, with examples for better understanding.Syntax: dim(x)Param
2 min read
Convert a matrix into a lower triangular matrix using R In this article, we will explore various methods to convert a matrix into a lower triangular matrix in R programming language. What is a matrix?A matrix is a two-dimensional rectangular data structure, which is a collection of rows and columns. Representation of rows is horizontal and columns are ve
5 min read