Skip to content

R: matrix sizes

3 messages · Clark Allan, Uwe Ligges, Adaikalavan Ramasamy

#
hi all

assume that one is doing a simulation. in each iteration one produces a
vector of results. this vectors length might change for each different
iteration. how can one construct a matrix that contains all of the
interation results in a matrix where each of the columns are the outputs
from the different interations.

how would have to define the output matrix initally?

/
thanking you in advance
#
Clark Allan wrote:

            
Of course, you define it to the maximal_length x number_iterations, but 
in fact you probably want a list rather than a matrix.

Uwe Ligges
#
1) If the output at each iteration gives a fixed number of elements,
then you can pre-define the matrix. For example

   mat <- matrix( NA, nr=6, nc=500 )
   for(i in 1:500 ){ 
      x <- rnorm(13)
      mat[ , i] <- summary(x)
   }

2) If the length of the output varies at each iteration, then it is
probably best to use a list.

   mylist <- list(NULL)
   for(i in 1:500){
       x <- rpois(1, lambda=10) + 1
       y <- rnorm(x)
       my.list[[ i ]] <- y
   }

Regards, Adai
On Mon, 2005-08-08 at 12:34 +0200, Clark Allan wrote: