Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- visit my blog: http://reakkt.com
- ## StretchVector
- # x - rozciagany wektor
- # n - docelowa dlugosc wektora
- StretchVector <- function(x,n) {
- k <- length(x)
- if (k>1) {
- to.fill <- n-k
- fill.avg <- ceiling(n/k)
- fill.vector <- rep(fill.avg,k)
- while (sum(fill.vector)!=n) {
- # znak wymaganej korekty
- correction.sign <- ifelse(sum(fill.vector)>n,-1,+1)
- # wielkosc wymaganej korekty
- correction.needed <- sum(fill.vector)-n
- # srednia wymagana korekta
- correction.avg <- ceiling(correction.needed/k)
- # inicjalizacja wektora korekcyjnego
- correction.vector <- rep(1,correction.needed)
- # losowanie pozycji 'fill.vector', ktore maja zostac skorygowane
- correction.positions <- sample(1:k,correction.needed)
- fill.vector[correction.positions] <- fill.vector[correction.positions]+correction.vector*correction.sign
- }
- } else fill.vector <- n
- # wlasciwe rozciaganie wektora 'x'
- x.stretched <- NULL
- for (i in 1:k) x.stretched <- c(x.stretched,rep(x[i],fill.vector[i]))
- return( list(x=x.stretched, v=fill.vector) )
- }
- # najdluzszy wektor:
- x1l <- round(runif(1,100,200))
- n <- x1l
- x2l <- round(runif(1,3,n*0.9))
- x3l <- round(runif(1,3,n*0.9))
- x1 <- cumsum(rnorm(x1l))
- x2 <- cumsum(rnorm(x2l))
- x3 <- cumsum(rnorm(x3l))
- x2s <- StretchVector(x2,n)
- x3s <- StretchVector(x3,n)
- # zaznaczanie oryginalnych punktow wektorow:
- x2s$o <- NULL; for (i in 1:length(x2s$v)) x2s$o <- c(x2s$o,x2[i],rep(NA,x2s$v[i]-1))
- x3s$o <- NULL; for (i in 1:length(x3s$v)) x3s$o <- c(x3s$o,x3[i],rep(NA,x3s$v[i]-1))
- x.all <- cbind(x1,x2s$x,x3s$x)
- matplot(x.all,type="l",lty="solid")
- points(x2s$o,col="Red")
- points(x3s$o,col="Green")
- # srednia:
- lines(apply(x.all, 1, mean),type="l",lty="dotted",lwd=4,col="Black")
- # oryginalne dlugosci wektorow:
- n;x2l;x3l
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement