[R] elegant matrix creation

From: Robin Hankin <r.hankin_at_noc.soton.ac.uk>
Date: Tue 12 Jul 2005 - 21:11:47 EST


Hi

I want to write a little function that takes a vector of arbitrary length "n" and returns a matrix of size n+1 by n+1.

I can't easily describe it, but the following function that works for n=3 should convey what I'm trying to do:

f <- function(x){

   matrix(c(

    1           ,   0      ,   0 , 0,
x[1]          ,   1      ,   0 , 0,
x[1]*x[2]     , x[2]     ,   1 , 0,

x[1]*x[2]*x[3], x[2]*x[3], x[3], 1
),
4,4, byrow=T)
}

f(c(10,7,2))

      [,1] [,2] [,3] [,4]

[1,]    1    0    0    0
[2,]   10    1    0    0
[3,]   70    7    1    0
[4,]  140   14    2    1

>

As one goes down column "i", the entries get multiplied by successive elements of x, starting with x[i], after the first "1"

As one goes along a row, one takes a product of the tail end of x, until the zeroes kick in.

Am I missing some clever solution?

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Jul 12 21:16:33 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:32 EST