Home > Mobile >  How to create this sequence in R
How to create this sequence in R

Time:12-24

enter image description here

> for(i in 1:12){
      x<-0.1
      h<-36
      y<-(x^h)*((x*2)^(h-2))
      x<-x*2
      h<-h-3
      print(y)
  }

This is what ive tried so far, doesnt seem to work, but i believe that the logic behind it is correct.

CodePudding user response:

R is vectorized, so here you might not need a for loop to create the sequence:

s <- head(tail(rep(seq(0, 12), each = 2), -1), -1)
# [1]  0  1  1  2  2  3  3  4  4  5  5  6  6  7  7  8  8  9  9 10 10 11 11 12

x = 0.1 * 2^s
#[1]   0.1   0.2   0.2   0.4   0.4   0.8   0.8   1.6   1.6   3.2   3.2   6.4   6.4  12.8  12.8  25.6  25.6  51.2  51.2 102.4 102.4 204.8 204.8 409.6

h = seq(36, 1)[c(T, F, T)]
# [1] 36 34 33 31 30 28 27 25 24 22 21 19 18 16 15 13 12 10  9  7  6  4  3  1

x^h

CodePudding user response:

You can try

n <- 12
x <- (2^(seq(n) - 1) / 10)^seq(3 * n, 3, -3)
y <- (2^seq(n) / 10)^seq(3 * n - 2, 1, -3)
z <- x * y
  • Related