2
I am trying to create a method for the Minkowsky formula but it is giving this error. It happens in 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 differently, doing matriz_distancia[i,] <- formula
, but at each iteration a whole new line was generated at the i position
For example, in the first iteration of the internal 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 generates this:
0.8917289 0.377533 0.8917289 0.377533 0.8917289 0.377533 0.8917289 0.377533 0.8917289 0.377533
I don’t understand why this happens, I’m not familiar with R. It’s generating repeated values
Thank you so much! You helped so much :D
– Marcio