##########   EXAMPLE 1   ##########
X <- matrix(rnorm(2000),100,20)
Xr <- nscale(X,mode=2)          # scale columns to ssnew=1
colSums(Xr^2)
Xr <- nscale(X,mode=2,ssnew=2)  # scale columns to ssnew=2
colSums(Xr^2)
##########   EXAMPLE 2   ##########
Xold <- X <- matrix(rnorm(400),20,20)
iter <- 0
chk <- 1
# iterative rescaling of modes 1 and 2
while(iter<500 & chk>=10^-9){
  Xr <- nscale(Xold,mode=1)
  Xr <- nscale(Xr,mode=2)
  chk <- sum((Xold-Xr)^2)
  Xold <- Xr
  iter <- iter + 1
}
iter
rowSums(Xr^2)
colSums(Xr^2)
##########   EXAMPLE 3   ##########
X <- array(rnorm(20000),dim=c(100,20,10))
Xc <- nscale(X,mode=2)   # scale within columns
rowSums(aperm(Xc,perm=c(2,1,3))^2)
##########   EXAMPLE 4   ##########
X <- array(rnorm(100000),dim=c(100,20,10,5))
Xc <- nscale(X,mode=4)   # scale across 4-th mode
rowSums(aperm(Xc,perm=c(4,1,2,3))^2)
##########   EXAMPLE 5   ##########
X <- replicate(5,array(rnorm(20000),dim=c(100,20,10)),simplify=FALSE)
Xc <- nscale(X,ssnew=(20*10*5))   # mean square of 1
rowSums(sapply(Xc, function(x) rowSums(x^2))) / (20*10*5)
Run the code above in your browser using DataLab