[R] Matrix

Mathew Guilfoyle mrguilfoyle at gmail.com
Mon Mar 6 22:10:33 CET 2017

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)


> On 6 Mar 2017, at 16:18, Peter Thuresson <peter.thuresson at umea.se> 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]]
> ______________________________________________
> R-help at r-project.org 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.

More information about the R-help mailing list