How to standardize matrix elements in R?

The standardization is the process of converting a value to another value so that the mean of the set of values from which the original value was taken becomes zero and the standard deviation becomes one. To standardize matrix elements, we can use data.Normalization function of clusterSim package but we need to make sure that we set the type argument to n1 because that corresponds to standardization with mean zero and standard deviation 1.
Loading clusterSim package −
library("clusterSim")
 Live Demo
M1<-matrix(rnorm(25,5,1),ncol=5)
M1
[,1] [,2] [,3] [,4] [,5]
[1,] 5.556224 2.934854 6.239076 4.501244 5.697287
[2,] 5.663404 4.404059 4.458465 2.875686 2.939572
[3,] 4.254188 4.168798 5.716965 5.003396 5.501523
[4,] 4.720976 5.032672 5.511445 4.678973 5.289942
[5,] 2.882521 5.694891 4.996887 4.825759 3.951424
data.Normalization(M1,type="n1")
[,1] [,2] [,3] [,4] [,5]
[1,] -0.84326235 -1.4331856 0.03959949 0.006214853 -0.6799208
[2,] 0.93062056 0.5714407 -0.31945831 0.065871281 -0.7808809
[3,] -1.18376086 -0.6408459 0.33301120 -0.026702496 -0.6877277
[4,] 1.00673967 0.5514687 -1.40208868 -1.435823004 1.3452576
[5,] 0.08966297 0.9511221 1.34893630 1.390439366 0.8032717
attr(,"normalized:shift")
1 2 3 4 5
5.473020 4.598571 5.143872 4.673848 4.880121
attr(,"normalized:scale")
1 2 3 4 5
1.0620129 0.9269254 0.9739280 1.2254604 0.9488868
 Live Demo
M2<-matrix(rpois(100,10),ncol=10)
M2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 15 10 14 15 5 3 11 11 11 7
[2,] 10 8 6 13 4 15 8 6 13 14
[3,] 2 10 5 15 4 10 9 7 6 13
[4,] 12 5 14 11 7 13 8 12 8 7
[5,] 11 11 12 15 10 9 9 12 19 8
[6,] 10 12 8 9 6 12 10 15 11 10
[7,] 13 12 11 9 7 8 17 17 18 13
[8,] 11 8 8 6 10 6 9 8 13 12
[9,] 9 8 10 13 12 14 8 7 4 13
[10,] 7 10 4 7 14 8 10 13 11 11
data.Normalization(M2,type="n1")
data.Normalization(M2,type="n1")
        [,1]       [,2]        [,3]         [,4]          [,5]     [,6]
[1,]  -1.9409899  -0.8923761  1.86543426  -1.67507682  -1.1484061  -1.4356319
[2,]  0.9704950  1.5101749  -0.64037295  -0.09481567  -1.6477131  -0.8114441
[3,]  0.3234983  0.1372886  1.03016519  -0.09481567   1.8474359   -0.4993502
[4,]  0.3234983  0.8237318  -0.64037295  -1.04297236   0.3495149   2.3094948
[5,]  0.9704950  0.1372886  0.75174216  0.53728879    -0.1497921  -0.1872563
[6,]  -0.3234983  1.5101749  0.19489612  -0.09481567    0.8488219   0.4369314
[7,] -0.6469966  -0.5491545  -1.47564202  -0.41086790  -0.6490991 -0.4993502
[8,] -0.9704950  -1.2355977  -0.08352691  -0.09481567  -0.1497921  0.4369314
[9,]  0.0000000  -0.8923761  -0.91879598  1.80149771    0.3495149   0.1248376
[10,]  1.2939933  -0.5491545  -0.08352691  1.16939325  0.3495149   0.1248376
     [,7]         [,8]       [,9]     [,10]
[1,] -0.9091373 2.07152663 0.76931647 1.4367622
[2,] 0.6060915 0.74362494 -0.62944075 -0.2535463
[3,] 1.2121831 -1.11543742 0.06993786 0.1690309
[4,] -1.5152288 0.21246427 0.06993786 -0.6761234
[5,] -0.6060915 -0.84985708 -0.27975144 -1.5212777
[6,] -0.3030458 0.47804461 -1.67850865 -0.6761234
[7,] -0.9091373 -0.05311607 0.06993786 0.5916080
[8,] 1.5152288 -0.05311607 0.06993786 1.0141851
[9,] 0.3030458 -0.05311607 2.16807368 1.0141851
[10,] 0.6060915 -1.38101775 -0.62944075 -1.0987005
attr(,"normalized:shift")
1 2 3 4 5 6 7 8 9 10 12.0 10.6 10.3 9.3 10.3 10.6 10.0 9.2 8.8 8.6
attr(,"normalized:scale")
1 2 3 4 5 6 7 8
3.091206 2.913570 3.591657 3.164034 2.002776 3.204164 3.299832 3.765339
9 10
2.859681 2.366432
 Live Demo
M3<-matrix(round(runif(36,2,10),0),ncol=6)
M3
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   4    9    4    8    7    5
[2,]   8    3    9    7    9    3
[3,]   9    3    8    4    9    4
[4,]   6   10    4    7    3    3
[5,]   7    8   10    9    4    6
[6,]   7    9    6    9    3    7
data.Normalization(M3,type="n1")
      [,1]        [,2]       [,3]        [,4]      [,5]        [,6]
[1,]  0.8017837  1.4647150  1.5430335 -0.6358384  0.1331559  0.8017837
[2,] -1.3363062 -1.3315591 -0.7715167 -1.1808427  0.9320914 -0.2672612
[3,] -0.8017837 -0.5326236  0.1543033  1.5441789 -0.2663118 -0.8017837
[4,] -0.2672612 -0.5326236 -0.3086067  0.4541703 -1.0652473 -1.3363062
[5,]  0.2672612  0.6657796 -1.2344268 -0.6358384 -1.0652473  0.2672612
[6,]  1.3363062  0.2663118  0.6172134  0.4541703  1.3315591  1.3363062
attr(,"normalized:shift")
1 2 3 4 5 6
5.500000 5.333333 4.666667 5.166667 6.666667 6.500000
attr(,"normalized:scale")
1 2 3 4 5 6
1.870829 2.503331 2.160247 1.834848 2.503331 1.870829
 Live Demo
M4<-matrix(rexp(16,0.50),nrow=4)
M4
        [,1]      [,2]      [,3]     [,4]
[1,] 1.8392684 0.1260047 1.8536475 0.3727895
[2,] 2.3926115 2.9282159 0.5356917 0.6675259
[3,] 0.6198705 5.3994087 0.7795360 1.6238094
[4,] 3.9293381 0.6119497 0.8212652 0.6498672
data.Normalization(M4,type="n1")
           [,1]       [,2]       [,3]        [,4]
[1,] -0.76247841   0.6334982  0.2251928  1.2625561
[2,] -0.08745082  -1.0914747 -0.8013569 -0.6296948
[3,]  1.43733539  -0.5780098 -0.7465997 -0.9525044
[4,] -0.58740616   1.0359864  1.3227638  0.3196432
attr(,"normalized:shift")
1 2 3 4
1.587821 1.762592 2.272075 3.611091
attr(,"normalized:scale")
1 2 3 4
0.4923935 1.5823407 2.2370054 1.3130271