# Matrix

9 messages
Open this post in threaded view
|

## Matrix

 Hello, Is there a function in R which can transform, let say a vector: c(1:4) to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. I want the output below from the vector above, like this: p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) matrix(p,7,4) best regards / Peter         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Matrix

 ## 1. ## This could be captured into a function tmp <- matrix(0, 7, 4) tmp diag(tmp) <- 1 diag(tmp[-1,]) <- 2 diag(tmp[-(1:2),]) <- 3 diag(tmp[-(1:3),]) <- 4 tmp ## 2. v <- 1:4 v2 <- c(v, rep(0, length(v))) ## this generates a warning that can safely be ignored (or turned off) matrix(v2, length(v2)-1, length(v)) On Mon, Mar 6, 2017 at 11:18 AM, Peter Thuresson <[hidden email]> wrote: > Hello, > > Is there a function in R which can transform, let say a vector: > > c(1:4) > > to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. > I want the output below from the vector above, like this: > > p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) > > matrix(p,7,4) > > best regards / Peter > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Matrix

 In reply to this post by Peter Thuresson How about this: p0 <- 1:4 matrix( c( rep( c(p0, rep(0, 4)) , times=3) , p0) , 7, 4) Of course, it would take some effort to generalize it to different lengths for p0. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 3/6/17, 8:18 AM, "R-help on behalf of Peter Thuresson" <[hidden email] on behalf of [hidden email]> wrote:     Hello,         Is there a function in R which can transform, let say a vector:         c(1:4)         to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column.     I want the output below from the vector above, like this:         p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4)         matrix(p,7,4)         best regards / Peter         [[alternative HTML version deleted]]         ______________________________________________     [hidden email] mailing list -- To UNSUBSCRIBE and more, see     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.     ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Matrix

 In reply to this post by Peter Thuresson Hello, Try the following. proj <- function(x){         n <- length(x)         y <- numeric(n)         z <- rep(c(x, y), times = n)         z <- z[-((length(z) - n + 1):length(z))]         matrix(z, ncol = n) } proj(1:4) proj(1:5) Hope this helps, Rui Barradas Em 06-03-2017 16:18, Peter Thuresson escreveu: > Hello, > > Is there a function in R which can transform, let say a vector: > > c(1:4) > > to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. > I want the output below from the vector above, like this: > > p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) > > matrix(p,7,4) > > best regards / Peter > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Matrix

Open this post in threaded view
|

## Re: Matrix

 Well, of course,  I *did* make a dumb error (again!!). Here's the corrected version: x <- c(1:5,10:12) ## generate vector of indices by outer and %% i <- seq_along(x) nc <- 4 ## number of columns desired #### Corrected statement #### indx <- (outer(i, rev(i-1),"+") %% length(x)) [,seq_len(nc)] +1 #####  Corrected statement matrix(x[indx],ncol = nc) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Mar 6, 2017 at 11:00 AM, Bert Gunter <[hidden email]> wrote: > Clever, Don. > > Here's a more explicit approach that generalizes (if I haven't made > any dumb errors): > > x <- c(1:5,10:12) > ## generate vector of indices by outer and %% > i <- seq_along(x) > nc <- 4 ## number of columns desired > ## get subscripting indices via outer() and %% > indx <- outer(i,rev(i),"+") %% (length(x))[,seq_len(nc)]+1 > matrix(x[indx],ncol = nc) > > Cheers, > Bert > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Mon, Mar 6, 2017 at 9:46 AM, MacQueen, Don <[hidden email]> wrote: >> How about this: >> >> p0 <- 1:4 >> >> matrix( c( rep( c(p0, rep(0, 4)) , times=3) , p0) , 7, 4) >> >> Of course, it would take some effort to generalize it to different lengths for p0. >> >> -Don >> >> -- >> Don MacQueen >> >> Lawrence Livermore National Laboratory >> 7000 East Ave., L-627 >> Livermore, CA 94550 >> 925-423-1062 >> >> >> On 3/6/17, 8:18 AM, "R-help on behalf of Peter Thuresson" <[hidden email] on behalf of [hidden email]> wrote: >> >>     Hello, >> >>     Is there a function in R which can transform, let say a vector: >> >>     c(1:4) >> >>     to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. >>     I want the output below from the vector above, like this: >> >>     p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) >> >>     matrix(p,7,4) >> >>     best regards / Peter >> >>         [[alternative HTML version deleted]] >> >>     ______________________________________________ >>     [hidden email] mailing list -- To UNSUBSCRIBE and more, see >>     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. >> >> >> ______________________________________________ >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >> 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Matrix

 In reply to this post by Peter Thuresson Effectively you want a circulant matrix but filled by column. Example input vector and number of columns x = c(1:8,19:20) nc = 5 For the result you specifically describe, the following generalises for any vector and any arbitrary number of columns 'nc', padding with zeros as necessary. matrix(c(rep(c(x,rep(0,nc)),nc-1),x), ncol=nc) For the unpadded column circulant on the same inputs nx = length(x) ind = 1+ outer(0:(nx-1),0:(1-nc),'+') %% nx matrix(x[ind], ncol=nc) Cheers > On 6 Mar 2017, at 16:18, Peter Thuresson <[hidden email]> wrote: > > Hello, > > Is there a function in R which can transform, let say a vector: > > c(1:4) > > to a matrix where the vector is repeated but "projected" +1 one step down for every (new) column. > I want the output below from the vector above, like this: > > p<-c(1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3,4) > > matrix(p,7,4) > > best regards / Peter > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.