sames.st.end <- function(x){
diff.x <- diff(x)
s.x <- sign(abs(diff.x))
diff.s.x <- diff(s.x)
st <- 1
end <- c()
init <- 0
for(i in 1:length(diff.s.x)){
if(diff.s.x[i]==-1){
if(init==0){
st <- i+1
}else{
st <- c(st,i+1)
}
}else if(diff.s.x[i]==1){
if(init==0){
init <- 1
}
end <- c(end,i+1)
}
}
if(length(st) > length(end)){
end <- c(end,length(x))
}
cbind(st,end)
}
s <- sort(runif(10))
x <- c()
n <- sample(1:5,length(s),replace=TRUE)
for(i in 1:length(s)){
x <- c(x,rep(s[i],n[i]))
}
x
out.same <- sames.st.end(x)
for(i in 1:length(out.same[,1])){
print(x[out.same[i,1]:out.same[i,2]])
}
> x
[1] 0.1795974 0.1795974 0.1795974 0.1795974 0.3117404 0.3466666 0.3466666
[8] 0.4574810 0.4778495 0.4778495 0.5505267 0.5505267 0.5505267 0.5505267
[15] 0.7426378 0.7458155 0.7587828 0.7587828 0.8995060
> out.same <- sames.st.end(x)
[1] 0.1795974 0.1795974 0.1795974 0.1795974
[1] 0.3466666 0.3466666
[1] 0.4778495 0.4778495
[1] 0.5505267 0.5505267 0.5505267 0.5505267
[1] 0.7587828 0.7587828