5
Reformulating the question, I would like to implement the following equation:

Where D is a matrix of order i x k x l, lambda is a matrix k x j and pk = {1,2,3,4,5,6}.
For this I need some function that operates with three-dimensional matrix instead of nested loops.
Below complement posted by the author (probably because we don’t have enough data to be sure):
library(KRLS)
matriz = matrix(c(rnorm(160)), ncol = 8)
matriz = (matriz - mean(matriz))/sd(matriz)
Separating in partitions
particao_1 = matrix(rep(0, length(matriz[1,])*length(matriz[,1])), ncol = length(matriz[1,]))
particao_2 = matrix(rep(0, length(matriz[1,])*length(matriz[,1])), ncol = length(matriz[1,]))
particao_3 = matrix(rep(0, length(matriz[1,])*length(matriz[,1])), ncol = length(matriz[1,]))
Random selection of the partition by kmeans
inicio = kmeans(matriz, 3)
i= 1;
for(i in 1:length(inicio$cluster)){
    if(inicio$cluster[i] == 1) particao_1[i,] = matriz[i,]
    else if(inicio$cluster[i] == 2) particao_2[i,] = matriz[i,]
    else  particao_3[i,] = matriz[i,]
  i = i +1
}
removing the zero lines
particao_1 = particao_1[!(apply(particao_1, 1, function(y) any(y == 0))),]
particao_2 = particao_2[!(apply(particao_2, 1, function(y) any(y == 0))),]
particao_3 = particao_3[!(apply(particao_3, 1, function(y) any(y == 0))),]
selecao = sample(1:3, 1)
(
  if(selecao == 1) particao.selecionada = particao_1
  else if(selecao == 2) particao.selecionada = particao_2
  else  particao.selecionada = particao_3
)
p = length(particao.selecionada[1,]) #Numero de atributos = p
Calculating Sigma
sigma = c()
q10 = c()
q90 = c()
i = 1; j = 1; k = 1
for(i in 1 : length(matriz[,1])){
  for(k in 1 : length(particao.selecionada[,1])){
    if((matriz[i,] == particao.selecionada[k,])[1]){      
     for(j in 1 : p){
        q10[j] = quantile(dist(matrix(c(matriz[,j], particao.selecionada[,j]), ncol = 2), 
                            method = "euclidean")^2, 0.1)
        q90[j] = quantile(dist(matrix(c(matriz[,j], particao.selecionada[,j]), ncol = 2), 
                            method = "euclidean")^2, 0.9)
        sigma[j] = (q10[j] + q90[j])/2
      }
    }
  }
}
Assembling the kernel matrix
matriz.kernel = matrix(rep(0, length(matriz[,1])*length(matriz[,1])), ncol = length(matriz[,1]))
for(i in 1:p){
  matriz.kernel = gausskernel(matriz, sigma[j])
}
Mounting the Event (d)
#--- Montando a equacao d.1 ---#
d.1 = array(rep(0, length(matriz[,1])*length(particao.selecionada[,1])*p),
              dim = c(length(matriz[,1]), length(particao.selecionada[,1]), p))
for(j in 1: length(matriz[,1])){
  for(k in 1: length(particao.selecionada[,1])){
    for(i in 1 : length(matriz[,1])){
      if(i == j){
        eq6.1[i,k, ] = matriz.kernel[i,j]
      }
    }
  }
}
Mounting the Equancy d.2
d.2.temp = array(rep(0, length(particao.selecionada[,1])*p), 
              dim = c(length(particao.selecionada[,1]), p))
d.2 = c()
for(j in 1: length(matriz[1,])){
  for(k in 1: length(particao.selecionada[,1])){
    for(i in 1 : length(matriz[,1])){
      if((matriz[i,] == particao.selecionada[k,])[1]){
        eq6.2.temp[k,j] = matriz.kernel[k,j]
        eq6.2[j] = 2*sum(eq6.2.temp[,j])/length(particao.selecionada[,1])
      }
    }
  }
}
Mounting the Equancy d.3
d.3.temp = array(rep(0, length(particao.selecionada[,1])*p), 
                   dim = c(length(particao.selecionada[,1]), p))
d.3 = 0
for(j in 1: length(matriz[1,])){  
  for(i in 1 : length(matriz[,1])){
    if((matriz[i,] == particao.selecionada[k,])[1]){
      for(k in 1: length(particao.selecionada[,1])){
        eq6.3.temp[k,j] = matriz.kernel[k,j]
        eq6.3 = sum(eq6.3.temp)/length(particao.selecionada[,1])^2
      }        
    }
  }
}
rm(d.3.temp)
n = 1
d.12 = array(rep(0, length(matriz[,1])*length(particao.selecionada[,1])*p),
               dim = c(length(matriz[,1]), length(particao.selecionada[,1]), p))
while(n <= 8)
{
  d.12[, , n] = eq6.1[, , n] - eq6.2[n]
  n = n + 1
}
d = d.12 + d.3
rm(d.12)
The lambda is a matrix of length k, j where k = length(selected partition.[,1] and p = length(matrix[,1]. I need to operate with the three-dimensional matrix to solve the lambda equation.
What error message you are receiving?
– Carlos Cinelli
Please enter an example of the original data and the expected result. Certainly the answer is not to use multiple loops for nesting, R has tools to work with vectorized matrices. One of them is the operator
%*%that does matrix multiplication instead of scaling.– Molx
out of context it is very difficult to understand this formula... which is what there?
– Daniel Falbel
Can do
mD <- d ;
dms=dim(mD);
pk=1:dms[1];
dl=dms[3];
lp=1:dland use the code below to find lambda– Robert