I'm trying to do a for loop over 6000000 or so and they are all computed independently so I wonder if I can replace it with foreach.
p.s : The TIME_SERIES just like data.frame(VALUE=C(1,2,3,4,5),DOY=c(13,25,36,54,66))
for (i in 1:ncell(cor_raster)) {
TIME_SERIES<-GET_CELL_VALUE(YEAR = 2015,BASE_PATH = "G:/LANDSAT/PATH146ROW036/MEAN",number = i,MAT = MAT_2015) ##Get the time series of points and their corresponding times,return a data frame
if(any(is.na(TIME_SERIES))){
FDD_MAT[i]<-NA
TDD_MAT[i]<-NA
}else{
some calculate funtion and return a vector,such as c(1,2)
FDD_MAT[i]<-RETURN_VECTOR[1]
TDD_MAT[i]<-RETURN_VECTOR[2]
}
}
However, I found very few examples where the custom function is called in the foreach, so I am confused how to edit the foreach code. At the same time I tried editing the code with foreach, but it didn't work, it is attached below,How should I modify it?
cl<-cl.cores-1
cl <- makeCluster(cl.cores)
registerDoParallel(cl <- makeCluster(cl))
result<-list()
ss<-foreach(i=1:ncell(cor_raster),.combine = "rbind",.inorder = TRUE)%do%{
TIME_SERIES<-GET_CELL_VALUE(YEAR = 2015,BASE_PATH = "G:/LANDSAT/PATH146ROW036/MEAN",number = i,MAT = MAT_2015)
if(any(is.na(TIME_SERIES))){
FDD_MAT[i]<-NA
TDD_MAT[i]<-NA
}else{
some calculate funtion and return a ve,such as c(1,2)
FDD_MAT[i]<-RETURN_VECTOR[1]
TDD_MAT[i]<-RETURN_VECTOR[2]
}
result[[1]]<-TDD_MAT
result[[2]]<-FDD_MAT
return(result)
}