简体   繁体   中英

R : subscript out of bounds

I have to realize the kohonen algorithm on 4*4 dimension. This one works but I have a small problem because I get this error:

######################################
## Initialize random matrix phi/psi ##
######################################

##Pour mac
setwd("/Users/amandinelecerfdefer/Desktop/Kohonen_MAP")
data_phipsi<-read.csv("/Users/amandinelecerfdefer/Desktop/Kohonen_MAP/pbA.txt.new.2.rep30.phipsi",h=F)

colnames(data_phipsi)<-c("phi1","psi2","phy2","psi3","phy3","psi4","phy4","psi5")

random_list<-list()
borne<-16 
for(n in 1:borne){
  random_list[[n]]<-floor(runif(8,min=-180,max=180)) 
}

# ##############Print random matrix#####
# for(n in 1:16){
#   print(random_list[[n]])
# }

#creation of a matrix 4*4 representing the Kohonen map containing the vectors of the angles phi/psi
Kohonen_matrix<-matrix(random_list,ncol=sqrt(borne),nrow=sqrt(borne))
rownames(Kohonen_matrix)<-c('1','2','3','4')
colnames(Kohonen_matrix)<-c('1','2','3','4')


#############################################
##Function for distance calculation (RMSDA)##
#############################################

RMSDA<-function(data_phipsi,Kohonen_matrix)
{
  difference<-data_phipsi-Kohonen_matrix 
  for(j in 1:length(difference)){
    if (difference[j]< -180) {difference[j]=difference[j]+360}
    if (difference[j]> +180) {difference[j]=difference[j]-360}
  }
  distance=mean(sqrt(difference^2))  
  return(distance) 
}

#######################
##LEARNING FUNCTION ##
#######################

learning<-function(initial_rate,iteration,data_phipsi){
  return(initial_rate/(1+(iteration/nrow(data_phipsi))))
}


##############################
## Program for Kohonen MAp ###
##############################

initial_rate=0.75 # Decrease with training time
initial_radius=2 # Decrease with training time
iteration=3   


for(step in 1:iteration)
{
  data_phipsi<-data_phipsi[sample(nrow(data_phipsi)),] # Sample vectors of training (samples of lines of the dataframe)
  print(step) #Visualize where we are in loops
  for(k_row in 1:nrow(data_phipsi))
  {
    #Update learn_rate and radius at each row of each iteration
    learn_rate<-learning(initial_rate,((step-1)*nrow(data_phipsi))+k_row,data_phipsi)
    learn_radius<-learning(initial_rate,((step-1)*nrow(data_phipsi))+k_row,data_phipsi)
    #Find distance between each vectors of angles of Kohonen Map and the training vector
    phipsi_RMSDA<-lapply(random_list, RMSDA, data_phipsi=data_phipsi[k_row,])


    #Unlist and create a matrix of distances
    vector_RMSDA<-unlist(phipsi_RMSDA)
    matrix_RMSDA<-matrix(vector_RMSDA,sqrt(borne),sqrt(borne))
    #Take the index of the winning neuron, it's the neuron that are more similar than the training vector
    win_index<-which(matrix_RMSDA==min(matrix_RMSDA), arr.ind=TRUE) 
    current_index<-list(c(1,1),c(2,1),c(3,1), c(4,1), c(1,2),c(2,2),c(3,2), c(4,2),c(1,3),c(2,3),c(3,3), c(4,3), c(1,4),c(2,4),c(3,4), c(4,4), c(1,4),c(2,4),c(3,4), c(4,4))



    #Update of angles of Kohonen Map vectors with the equation (be careful at number of parenthesis)
    for (mlist in 1:borne)
    {
       distance<-as.numeric(dist(rbind(win_index[1,], current_index[[mlist]])))
      random_list[[mlist]]<-(random_list[[mlist]]+ ( prot_phipsi[i_row,]-random_list[[mlist]])* (learn_rate*( exp (- ((distance)^2/(2*((learn_radius)^2)) )) ) ))
    }
    Kohonen_matrix<-matrix(random_list,sqrt(borne),sqrt(borne))
  } 
}

After running this code start, I get the error:

[1] 1 Error in win_index[1, ] : subscript out of bounds In addition: There were 16 warnings (use warnings() to see them)

Can you help me solve it?

Thank you. Thank you.

As the error indicates the index of win_index[1,] is out of bound, meaning that R cannot access the first-row element of that matrix, because it does not exist. You should double check if the matrix actually exists and has been initiated as I cannot find it in the code snippet you gave.

If you post the right code, we can have a better look.

Further, you receive other warnings - which might not be problematic. If you just type warnings() in your R console you see what's going on there.

See also Subscript out of bounds - general definition and solution? for further info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM