[R] How to 'explode' a matrix

2011-01-05 Thread Kevin Ummel
Hi everyone,

I'm looking for a way to 'explode' a matrix like this:

 matrix(1:4,2,2)
 [,1] [,2]
[1,]13
[2,]24

into a matrix like this:

 matrix(c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4),4,4)
 [,1] [,2] [,3] [,4]
[1,]1133
[2,]1133
[3,]2244
[4,]2244

My current kludge is this:

v1=rep(1:4,each=2,times=2)
v2=v1[order(rep(1:2,each=4,times=2))]
matrix(v2,4,4)

But I'm hoping there's a more efficient solution that I'm not aware of.

Many thanks,
Kevin

__
R-help@r-project.org 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.


Re: [R] How to 'explode' a matrix

2011-01-05 Thread Henrique Dallazuanna
Try this:

apply(apply(m, 2, rep, each = 2), 1, rep, each = 2)

or

m[rep(seq(nrow(m)), each = 2), rep(seq(ncol(m)), each = 2)]

On Wed, Jan 5, 2011 at 10:03 AM, Kevin Ummel kevinum...@gmail.com wrote:

 Hi everyone,

 I'm looking for a way to 'explode' a matrix like this:

  matrix(1:4,2,2)
 [,1] [,2]
 [1,]13
 [2,]24

 into a matrix like this:

  matrix(c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4),4,4)
 [,1] [,2] [,3] [,4]
 [1,]1133
 [2,]1133
 [3,]2244
 [4,]2244

 My current kludge is this:

 v1=rep(1:4,each=2,times=2)
 v2=v1[order(rep(1:2,each=4,times=2))]
 matrix(v2,4,4)

 But I'm hoping there's a more efficient solution that I'm not aware of.

 Many thanks,
 Kevin

 __
 R-help@r-project.org 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] How to 'explode' a matrix

2011-01-05 Thread Ben Bolker
Kevin Ummel kevinummel at gmail.com writes:

 I'm looking for a way to 'explode' a matrix like this:
 
  matrix(1:4,2,2)
  [,1] [,2]
 [1,]13
 [2,]24
 


  This is the Kronecker product of your matrix with the
matrix  (1 1 ; 1 1)

m - matrix(1:4,2,2)
kronecker(m,matrix(1,2,2))

  cheers
Ben Bolker

__
R-help@r-project.org 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.


Re: [R] How to 'explode' a matrix

2011-01-05 Thread Jorge Ivan Velez
Hi Kevin,

Take a look at

?kronecker

HTH,
Jorge


On Wed, Jan 5, 2011 at 7:03 AM, Kevin Ummel  wrote:

 Hi everyone,

 I'm looking for a way to 'explode' a matrix like this:

  matrix(1:4,2,2)
 [,1] [,2]
 [1,]13
 [2,]24

 into a matrix like this:

  matrix(c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4),4,4)
 [,1] [,2] [,3] [,4]
 [1,]1133
 [2,]1133
 [3,]2244
 [4,]2244

 My current kludge is this:

 v1=rep(1:4,each=2,times=2)
 v2=v1[order(rep(1:2,each=4,times=2))]
 matrix(v2,4,4)

 But I'm hoping there's a more efficient solution that I'm not aware of.

 Many thanks,
 Kevin

 __
 R-help@r-project.org 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.


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] How to 'explode' a matrix

2011-01-05 Thread Kevin Ummel
Thanks, Henrique.

The second option you suggested is about twice as fast as my original 
application.

Much appreciated,
Kevin

On Jan 5, 2011, at 6:30 PM, Henrique Dallazuanna wrote:

 Try this:
 
 apply(apply(m, 2, rep, each = 2), 1, rep, each = 2)
 
 or
 
 m[rep(seq(nrow(m)), each = 2), rep(seq(ncol(m)), each = 2)]
 
 On Wed, Jan 5, 2011 at 10:03 AM, Kevin Ummel kevinum...@gmail.com wrote:
 Hi everyone,
 
 I'm looking for a way to 'explode' a matrix like this:
 
  matrix(1:4,2,2)
 [,1] [,2]
 [1,]13
 [2,]24
 
 into a matrix like this:
 
  matrix(c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4),4,4)
 [,1] [,2] [,3] [,4]
 [1,]1133
 [2,]1133
 [3,]2244
 [4,]2244
 
 My current kludge is this:
 
 v1=rep(1:4,each=2,times=2)
 v2=v1[order(rep(1:2,each=4,times=2))]
 matrix(v2,4,4)
 
 But I'm hoping there's a more efficient solution that I'm not aware of.
 
 Many thanks,
 Kevin
 
 __
 R-help@r-project.org 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.
 
 
 
 -- 
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.