+ K = nrow(medoids)
+ synchrones = matrix(0, nrow=K, ncol=ncol(medoids))
+ counts = rep(0,K)
+ index = 1
+ repeat
+ {
+ range = (index-1) + seq_len(nb_series_per_chunk)
+ ref_series = getRefSeries(range)
+ if (is.null(ref_series))
+ break
+ #get medoids indices for this chunk of series
+ for (i in seq_len(nrow(ref_series)))
+ {
+ j = which.min( rowSums( sweep(medoids, 2, ref_series[i,], '-')^2 ) )
+ synchrones[j,] = synchrones[j,] + ref_series[i,]
+ counts[j] = counts[j] + 1
+ }
+ index = index + nb_series_per_chunk
+ }
+ #NOTE: odds for some clusters to be empty? (when series already come from stage 2)
+ # ...maybe; but let's hope resulting K1' be still quite bigger than K2
+ synchrones = sweep(synchrones, 1, counts, '/')
+ synchrones[ sapply(seq_len(K), function(i) all(!is.nan(synchrones[i,]))) , ]