shaowenhua at comcast.net
2007-Aug-16 03:10 UTC
[R] an easy way to construct this special matirx
Hi, Sorry if this is a repost. I searched but found no results. I am wondering if it is an easy way to construct the following matrix: r 1 0 0 0 r^2 r 1 0 0 r^3 r^2 r 1 0 r^4 r^3 r^2 r 1 where r could be any number. Thanks. Wen [[alternative HTML version deleted]]
On 16-Aug-07 03:10:17, shaowenhua at comcast.net wrote:> Hi, > Sorry if this is a repost. I searched but found no results. > I am wondering if it is an easy way to construct the following matrix: > > r 1 0 0 0 > r^2 r 1 0 0 > r^3 r^2 r 1 0 > r^4 r^3 r^2 r 1 > > where r could be any number. Thanks. > WenI dare say there's an even simpler way (and I feel certain someone will post one ... ); but (example): r<-0.1 r1<-r^((-3):0); r2<-r^(3:0) R<-r1%*%t(r2) R ## [,1] [,2] [,3] [,4] ##[1,] 1.000 10.00 100.0 1000 ##[2,] 0.100 1.00 10.0 100 ##[3,] 0.010 0.10 1.0 10 ##[4,] 0.001 0.01 0.1 1 R[upper.tri(R)]<-0 R ## [,1] [,2] [,3] [,4] ##[1,] 1.000 0.00 0.0 0 ##[2,] 0.100 1.00 0.0 0 ##[3,] 0.010 0.10 1.0 0 ##[4,] 0.001 0.01 0.1 1 Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 16-Aug-07 Time: 09:37:28 ------------------------------ XFMail ------------------------------
?toeplitz ?lower.tri since it is the lower triangle of a Toeplitz matrix (or drop the top row) r <- 0.95 R <- toeplitz(r^(0:4)) R[upper.tri(R)] <- 0 R[-1,] On Thu, 16 Aug 2007, shaowenhua at comcast.net wrote:> Hi, > Sorry if this is a repost. I searched but found no results. > I am wondering if it is an easy way to construct the following matrix: > > r 1 0 0 0 > r^2 r 1 0 0 > r^3 r^2 r 1 0 > r^4 r^3 r^2 r 1 > > where r could be any number. Thanks. > Wen-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Here are two solutions. In the first lo has TRUE on the lower diagonal and diagonal. Then we compute the exponents, multiplying by lo to zero out the upper triangle. In the second rn is a matrix of row numbers and rn >= t(rn) is the same as lo in the first solution. r <- 2; n <- 5 # test data lo <- lower.tri(diag(n), diag = TRUE) lo * r ^ (row(lo) - col(lo) + 1) Here is another one: rn <- row(diag(n)) (rn >= t(rn)) * r ^ (rn - t(rn) + 1) On 8/15/07, shaowenhua at comcast.net <shaowenhua at comcast.net> wrote:> Hi, > Sorry if this is a repost. I searched but found no results. > I am wondering if it is an easy way to construct the following matrix: > > r 1 0 0 0 > r^2 r 1 0 0 > r^3 r^2 r 1 0 > r^4 r^3 r^2 r 1 > > where r could be any number. Thanks. > Wen
Hi wen, I don't think it is easy to construct this matrix in a simple way. I tried and found a way to do it. Try the following codes: i<-1:4 j<-5 aa<-matrix(0,4,5) for (j in 1:5){aa[i,j]<-(i+1-j)} r<-4 #r could be any number bb<-r^aa bb[aa<0]=0 bb The matrix bb is what you want. Furthermore,I packaged this process into a function called mtrx as below: mtrx<-function(row,clm,r){ i<-1:row j<-clm aa<-matrix(row*clm,row,clm) for (j in 1:clm){aa[i,j]<-(i+1-j)} #r could be any number bb<-r^aa bb[aa<0]=0 bb } Now you can use the function to produce the matrix.The above-mentioned matrix is mtrx(4,5,4) Dejian Zhao On Thu, Aug 16, 2007 11:10, shaowenhua at comcast.net wrote:> Hi, > Sorry if this is a repost. I searched but found no results. > I am wondering if it is an easy way to construct the following > matrix: > > r 1 0 0 0 > r^2 r 1 0 0 > r^3 r^2 r 1 0 > r^4 r^3 r^2 r 1 > > where r could be any number. Thanks. > Wen > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code.-- De-Jian Zhao Institute of Zoology,Chinese Academy of Sciences +86-10-64807217 zhaodj at ioz.ac.cn
Hi Gabor, I am glad to see your answer,which gives a hope to resove this question in an easy way. I replied to this question in a more complex way before seeing your answer. However,I think your code needs some revision, because the original matrix is not a diagonal matrix. It has 4 rows and 5 columns.Looking forward to your revised codes. Best regards, On Thu, Aug 16, 2007 20:22, Gabor Grothendieck wrote:> Here are two solutions. In the first lo has TRUE on the lower > diagonal > and diagonal. Then we compute the exponents, multiplying by lo to > zero > out the upper triangle. In the second rn is a matrix of row numbers > and rn >= t(rn) is the same as lo in the first solution. > > r <- 2; n <- 5 # test data > > lo <- lower.tri(diag(n), diag = TRUE) > lo * r ^ (row(lo) - col(lo) + 1) > > Here is another one: > > rn <- row(diag(n)) > (rn >= t(rn)) * r ^ (rn - t(rn) + 1) > > On 8/15/07, shaowenhua at comcast.net <shaowenhua at comcast.net> wrote: >> Hi, >> Sorry if this is a repost. I searched but found no results. >> I am wondering if it is an easy way to construct the following >> matrix: >> >> r 1 0 0 0 >> r^2 r 1 0 0 >> r^3 r^2 r 1 0 >> r^4 r^3 r^2 r 1 >> >> where r could be any number. Thanks. >> Wen > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code. >-- De-Jian Zhao Institute of Zoology,Chinese Academy of Sciences +86-10-64807217 zhaodj at ioz.ac.cn
It was pointed out that the required matrix may not be square and the superdiagonal was missing in my prior post. Here is a revision: r <- 2; nr <- 4; nc <- 5 # test data x <- matrix(nr = nr, nc = nc) x <- row(x) - col(x) + 1 (x >= 0) * r ^ x On 8/16/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Here are two solutions. In the first lo has TRUE on the lower diagonal > and diagonal. Then we compute the exponents, multiplying by lo to zero > out the upper triangle. In the second rn is a matrix of row numbers > and rn >= t(rn) is the same as lo in the first solution. > > r <- 2; n <- 5 # test data > > lo <- lower.tri(diag(n), diag = TRUE) > lo * r ^ (row(lo) - col(lo) + 1) > > Here is another one: > > rn <- row(diag(n)) > (rn >= t(rn)) * r ^ (rn - t(rn) + 1) > > On 8/15/07, shaowenhua at comcast.net <shaowenhua at comcast.net> wrote: > > Hi, > > Sorry if this is a repost. I searched but found no results. > > I am wondering if it is an easy way to construct the following matrix: > > > > r 1 0 0 0 > > r^2 r 1 0 0 > > r^3 r^2 r 1 0 > > r^4 r^3 r^2 r 1 > > > > where r could be any number. Thanks. > > Wen >