Operation with three-dimensional arrays

Asked

Viewed 492 times

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?

  • 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.

  • 1

    out of context it is very difficult to understand this formula... which is what there?

  • Can do mD <- d ;&#xA;dms=dim(mD);&#xA;pk=1:dms[1];&#xA;dl=dms[3];&#xA;lp=1:dl and use the code below to find lambda

1 answer

1

Try this!

di=6
dk=5
dl=4
set.seed(234)
dados <- round(rnorm(di*dk*dl,20,5));
mD <- array(dados, c(di, dk, dl));  
mD[di,dk,dl-1] <- 24; #subst 18
pk=1:di
lp=1:dl
mDlp=lapply(lp,function(l)mD[,,l])
Numerador=Reduce('*',lapply(mDlp,function(Dp){
  Dpk=lapply(pk,function(i)Dp[i,])
  Reduce('+', Dpk)
}))^(1/dl)
mDpk=lapply(pk,function(i)mD[i,,])
Denominador=Reduce('+', mDpk)
lambda=Numerador/Denominador

The result is:

> lambda
          [,1]      [,2]      [,3]      [,4]
[1,] 1.0307199 0.9820245 1.0139603 0.9743524
[2,] 1.1486329 1.1064813 0.9068155 0.8676724
[3,] 0.9376565 0.9607136 0.9376565 1.1839097
[4,] 0.8895210 0.9997272 0.9655485 1.1646306
[5,] 1.0616960 1.0058172 0.9800271 0.9555264

Browser other questions tagged

You are not signed in. Login or sign up in order to post.