[R] Matrix: Problem with the code

markleeds at verizon.net markleeds at verizon.net
Sat Jan 10 03:53:57 CET 2009


Thanks Kingsford. I thought the column power was supposed to be just for 
that column but you're probably correct.  English has its oddities 
because if one reads the actual sentence the person wrote it's still not 
clear, atleast to me.

"Actually I want to have a matrix with p columns such that each column
will have the elements of  x^(column#)"

Thanks and apologies to Charlotte for my incorrect correction.



On Fri, Jan 9, 2009 at  9:37 PM, Kingsford Jones wrote:

> On Fri, Jan 9, 2009 at 6:36 PM,  <markleeds at verizon.net> wrote:
>> Charlotte: I ran your code because I wasn't clear on it and your way 
>> would
>> cause more matrices than the person requested.
>
> Bhargab gave us
>
>      x<-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
>
> and said: "I want to have a matrix with p columns such that each
> column will have the elements of  x^(column#)."
>
> so, I think Charlotte's code was spot-on:
>
> p <- 3
> outer(x, 1:p, '^')
>      [,1] [,2]   [,3]
>  [1,]   23  529  12167
>  [2,]   67 4489 300763
>  [3,]    2    4      8
>  [4,]   87 7569 658503
>  [5,]    9   81    729
>  [6,]   63 3969 250047
>  [7,]    8   64    512
>  [8,]    2    4      8
>  [9,]   35 1225  42875
> [10,]    6   36    216
> [11,]   91 8281 753571
> [12,]   41 1681  68921
> [13,]   22  484  10648
> [14,]    3    9     27
>
>
> Here's another way -- a bit less elegant, but a gentle
> introduction to thinking in vectors rather than elements:
>
>  mat <- matrix(0,nrow=length(x), ncol=p)
>
>  for(i in 1:p) mat[,i] <- x^i
>  mat
>      [,1] [,2]   [,3]
>  [1,]   23  529  12167
>  [2,]   67 4489 300763
>  [3,]    2    4      8
>  [4,]   87 7569 658503
>  [5,]    9   81    729
>  [6,]   63 3969 250047
>  [7,]    8   64    512
>  [8,]    2    4      8
>  [9,]   35 1225  42875
> [10,]    6   36    216
> [11,]   91 8281 753571
> [12,]   41 1681  68921
> [13,]   22  484  10648
> [14,]    3    9     27
>
>
> best,
>
> Kingsford Jones
>
>
>
>
>
>
> So
>> I think the code below it, although not too short, does what the 
>> person
>> asked. Thanks though because I understand outer better now.
>>
>> temp <- matrix(c(1,2,3,4,5,6),ncol=2)
>> print(temp)
>>
>> #One of those more elegant ways:
>> print(temp)
>> outer(temp,1:p,'^')One of those more elegant ways:
>>
>>
>> # THIS WAY I THINK GIVES WHAT THEY WANT
>>
>> sapply(1:ncol(temp), function(.col) {
>>  temp[,.col]^.col
>> })
>>
>>
>>
>> On Fri, Jan 9, 2009 at  7:40 PM, Charlotte Wickham 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:
>>>>
>>>> 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:
>>>>>
>>>>> Hi,
>>>>>
>>>>>
>>>>> Can any one please explain why the following code doesn't work? Or 
>>>>> can
>>>>> anyone suggest an alternative.
>>>>> Suppose
>>>>>      x<-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
>>>>>       mat<-0;
>>>>>       for(j in 1:length(x))
>>>>>       {
>>>>>          for(i in 1:p)
>>>>>               mat[i,j]<-x[j]^i;
>>>>>       }
>>>>>   Actually I want to have a matrix with p columns such that each 
>>>>> column
>>>>> will have the elements of  x^(column#).
>>>>>
>>>>> Thanks in advance.
>>>>>
>>>>> Bhargab
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Sarah Goslee
>>>> http://www.functionaldiversity.org
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>




More information about the R-help mailing list