问题描述:

I start with a vector

`x <- c(1:8)`

of eight components in R. So this vector looks like:

`[1] 1 2 3 4 5 6 7 8`

Then I compute a new value for x which I would like to store in rows. So, for intance, after three steps I would have a Matrix which is 3 times 8 and each row would contain the values for x for each step. That is

`[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]`

`[1,] 1 2 3 4 5 6 7 8`

`[2,] 9 10 11 12 13 14 15 16`

`[3,] 17 18 19 20 21 22 23 24`

where in the second and Third row we have added the new values, one by one.

Any suggestion how to do this? Thank you very much!

Try

```
n <- 3
matrix(x+ rep(seq(0,length.out=n, by=8), each=length(x)), nrow=n, byrow=TRUE)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,] 1 2 3 4 5 6 7 8
#[2,] 9 10 11 12 13 14 15 16
#[3,] 17 18 19 20 21 22 23 24
```

If you need double `for`

loops (it would have been better if you showed the code you used)

```
v1 <- seq(0, length.out=n, by=8)
m1 <- matrix(ncol=length(x), nrow=n)
for(i in 1:n){
for(j in seq_along(x)){
m1[i,j] <- v1[i]+x[j]
}
}
m1
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,] 1 2 3 4 5 6 7 8
#[2,] 9 10 11 12 13 14 15 16
#[3,] 17 18 19 20 21 22 23 24
```