I'm using R to generate two separate column vectors of length 20, and I want to add 150 additional column vectors to them and keep the new values based on values in previous columns. I initiate both column vectors using the code below:
n <- 20
set.seed(4)
x0 <- runif(n = n, min = -9, max = 9)
set.seed(16)
y0 <- runif(n = n, min = -9, max = 9)
This gives me the first column for each vector. Then, I generate a second column based on these values. However, I only want to keep the new values per row if they meet certain conditions. If they don't meet the conditions, I want the new column values to be replaced by the old column values. The code below is to generate the new column, analyze its values, and keep the new value or replace it with the old value, then repeat 150 iterations:
iter <- 150
for(j in 1:iter){
St <- runif(n = n, min = 0, max = 2)
dt <- runif(n = n, min = 0, max = 2*pi)
dx <- cos(dt)
dy <- sin(dt)
xm <- St*dx
ym <- St*dy
x0 <- cbind(x0, xm)
y0 <- cbind(y0, ym)
xall <- t(apply(x0, 1, cumsum))
yall <- t(apply(y0, 1, cumsum))
for(i in 1:n){
if(xall[i,j+1]<=3 & xall[i,j+1]>=-3 & yall[i,j+1]<=3 & yall[i,j+1]>=-3){
xall[i,j+1] <- xall[i,j+1]
yall[i,j+1] <- yall[i,j+1]
}
else if(xall[i,j]>3 | xall[i,j]<(-3) | yall[i,j]>3 | yall[i,j]<(-3)){
xall[i,j+1] <- xall[i,j+1]
yall[i,j+1] <- yall[i,j+1]
}
else{
xall[i,j+1] <- xall[i,j]
yall[i,j+1] <- yall[i,j]
}
}
}
The "xall" and "yall" arrays keep adding columns onto them, one by one for 150 iterations. After each iteration and a new column is added, it should follow these rules below:
- If both new values in the jth+1 column are between -3 and 3, take the new values no matter what, even if the current values in the jth column are also both within range
- If at least one current value in the jth column is outside this range, take the new values from the jth+1 column for both no matter what
- If the current values in the jth column are within range, but the new values in the jth+1 column are not, replace the new values in the jth+1 column with the current values in the jth column
- Finish comparing column values, generate a new column, then go through the rules again
I'll give an example output of my code here, which currently isn't working as needed:
> xall[3,10:14]
x10 x11 x12 x13 x14
-3.657078 -2.558799 -2.790860 -2.797736 -3.372856
> yall[3,10:14]
y10 y11 y12 y13 y14
-1.938531 -2.991856 -2.597014 -2.694228 -3.363116
Starting at x10 and y10. At least one of them is out of range [-3,3], so they should accept the new column value for column 11 (working as expected). In column 11, both x11 and y11 are within range, but the next column x12 and y12 are also both within range, so they should accept the new values (working as expected). Jumping to x13 and y13, both are within range, but at least one of the next values is not. So x14 and y14 should copy the previous values (not working as expected). The goal is to get something like this for x14 and y14:
> xall[3,10:14]
x10 x11 x12 x13 x14
-3.657078 -2.558799 -2.790860 -2.797736 -2.797736
> yall[3,10:14]
y10 y11 y12 y13 y14
-1.938531 -2.991856 -2.597014 -2.694228 -2.694228
This is essentially a random walk problem. Is there a way for each point to reach the goal of being between [-3,3] and then not leaving once arrived?
Aucun commentaire:
Enregistrer un commentaire