I'm trying to create a method for the minkowsky formula but it's giving this error. It happens on the line where I try to assign the value to matriz_distancia[i,j]
.
distancia_minkowsky <- function(xtreinamento, xteste, r){
matriz_distancia <- matrix(0, nrow(xteste), ncol = nrow(xtreinamento))
for(i in 1:nrow(xtest)){
for(j in 1:nrow(xtreinamento)){
matriz_distancia[i,j] <- (abs(xteste[i,] - xtreinamento[j,])^r)^(1/r)
}
}
return(matriz_distancia)
}
I had tried it the other way, doing matriz_distancia[i,] <- formula
, but at each iteration a whole new line was generated in position i
For example, in the first iteration of the inner loop generates this, for j = 10
4.680581 0.4122082 4.680581 0.4122082 4.680581 0.4122082 4.680581 0.4122082 4.680581 0.4122082
In the second iteration it generates this:
0.8917289 0.377533 0.8917289 0.377533 0.8917289 0.377533 0.8917289 0.377533 0.8917289 0.377533
I do not understand why this happens, I'm not familiar with R. You are generating repeated values