Matrix

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Matrix

Peter Thuresson
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.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix

Richard M. Heiberger
## 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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix

MacQueen, Don
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix

Rui Barradas
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix

Bert Gunter-2
In reply to this post by MacQueen, Don
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix

Bert Gunter-2
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix

Mathew Guilfoyle
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix

Gabor Grothendieck
In reply to this post by Peter Thuresson
Assuming that the input is x <- 1:4, try this one-liner:

> embed(c(0*x[-1], x, 0*x[-1]), 4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    2    1    0    0
[3,]    3    2    1    0
[4,]    4    3    2    1
[5,]    0    4    3    2
[6,]    0    0    4    3
[7,]    0    0    0    4

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)
>

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix

David Carlson
Change the "4" in embed(c(0*x[-1], x, 0*x[-1]), 4) to length(x) and it will generalize to other length vectors. This solution is not as compact, but it illustrates a relatively obscure R function:

> x <- 1:4
> ncol <- length(x)
> zeros <- rep(0, ncol - 1)
> toeplitz(c(zeros, x, zeros))[-(1:(ncol-1)), 1:ncol]
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    2    1    0    0
[3,]    3    2    1    0
[4,]    4    3    2    1
[5,]    0    4    3    2
[6,]    0    0    4    3
[7,]    0    0    0    4

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Gabor Grothendieck
Sent: Tuesday, March 7, 2017 7:21 AM
To: Peter Thuresson <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] Matrix

Assuming that the input is x <- 1:4, try this one-liner:

> embed(c(0*x[-1], x, 0*x[-1]), 4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    2    1    0    0
[3,]    3    2    1    0
[4,]    4    3    2    1
[5,]    0    4    3    2
[6,]    0    0    4    3
[7,]    0    0    0    4

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)
>

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[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.