Skip to content

Matrix: Problem with the code

3 messages · Bhargab Chattopadhyay, Sarah Goslee, Charlotte Wickham

#
Well, mat doesn't have any dimensions / isn't a matrix, and we don't
know what p is supposed to be. But leaving aside those little details,
do you perhaps want something like this:

      x<-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
      p <- 5
      mat<- matrix(0, nrow=p, ncol=length(x))
      for(j in 1:length(x))
      {
          for(i in 1:p)
              mat[i,j]<-x[j]^i
      }

Two notes: I didn't try it out, and if that's what you want rather
than a toy example
of a larger problem, there are more elegant ways to do it in R.

Sarah

On Fri, Jan 9, 2009 at 6:42 PM, Bhargab Chattopadhyay
<bhargab_1 at yahoo.com> wrote:

  
    
#
One of those more elegant ways:
outer(x, 1:p, "^")

Charlotte
On Fri, Jan 9, 2009 at 4:24 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote: