Re: [R] expand a matrix

2022-09-05 Thread Andrew Simmons
You can specify multiple indexes to replace at once, so you can avoid
a for loop entirely like this:
M <- matrix(nrow = 10, ncol = 10)
M[1:4, 5: 6] <- M[5: 6, 1:4] <- m[1, 2]
M[1:4,7] <- M[   7, 1:4] <- m[1, 3]
M[1:4, 8:10] <- M[8:10, 1:4] <- m[1, 4]
M[5:6,7] <- M[   7, 5:6] <- m[2, 3]
M[5:6, 8:10] <- M[8:10, 5:6] <- m[2, 4]
M[  7, 8:10] <- M[8:10,   7] <- m[3, 4]
M


You could also specify it directly with function 'matrix', it is
somewhat ugly though:
M <- matrix(c(
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
NA, NA, NA, NA, m[1,2], m[1,2], m[1,3], m[1,4],
m[1,4], m[1,4],
m[1,2], m[1,2], m[1,2], m[1,2], NA, NA, m[2,3], m[2,4],
m[2,4], m[2,4],
m[1,2], m[1,2], m[1,2], m[1,2], NA, NA, m[2,3], m[2,4],
m[2,4], m[2,4],
m[1,3], m[1,3], m[1,3], m[1,3], m[2,3], m[2,3], NA, m[3,4],
m[3,4], m[3,4],
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA,
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA,
m[1,4], m[1,4], m[1,4], m[1,4], m[2,4], m[2,4], m[3,4], NA,
 NA, NA
), nrow = 10, ncol = 10, byrow = TRUE)
M


If it interests you, you could take a look at package 'Matrix', it
contains classes for specific types of matrices, including upper
triangular matrices. This package helps to improve speed of linear
algebra computations and reduce the memory size of matrices.

On Mon, Sep 5, 2022 at 10:46 PM Jinsong Zhao  wrote:
>
> Hi there,
>
> I have a matrix likes:
>  > m
>   [,1] [,2] [,3] [,4]
> [1,]   NA123
> [2,]1   NA65
> [3,]26   NA4
> [4,]354   NA
>
> I hope to expand it to 10 by 10 matrix, M, likes:
>  > M
>[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>   [1,]   NA   NA   NA   NA11233 3
>   [2,]   NA   NA   NA   NA11233 3
>   [3,]   NA   NA   NA   NA11233 3
>   [4,]   NA   NA   NA   NA11233 3
>   [5,]1111   NA   NA655 5
>   [6,]1111   NA   NA655 5
>   [7,]222266   NA44 4
>   [8,]3333554   NA   NANA
>   [9,]3333554   NA   NANA
> [10,]3333554   NA   NANA
>
> I use the following code:
>
> M <- matrix(NA, 10, 10)
>
> for (i in 1:10) {
> for (j in 1:10) {
>if (i %in% 1:4 & j %in% 5:6) M[i,j] <- M[j,i] <- m[1,2]
>if (i %in% 1:4 & j == 7) M[i,j] <- M[j,i] <-m[1,3]
>if (i %in% 1:4 & j %in% 8:10) M[i,j] <- M[j,i] <-m[1,4]
>if (i %in% 5:6 & j == 7) M[i,j] <- M[j,i] <-m[2,3]
>if (i %in% 5:6 & j %in% 8:10) M[i,j] <- M[j,i] <-m[2,4]
>if (i == 7 & j %in% 8:10) M[i,j] <- M[j,i] <-m[3,4]
> }
> }
>
> Is there any convenience way to do it? Thanks!
>
> Best,
> Jinsong
>
> __
> R-help@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.

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


[R] expand a matrix

2022-09-05 Thread Jinsong Zhao

Hi there,

I have a matrix likes:
> m
 [,1] [,2] [,3] [,4]
[1,]   NA123
[2,]1   NA65
[3,]26   NA4
[4,]354   NA

I hope to expand it to 10 by 10 matrix, M, likes:
> M
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   NA   NA   NA   NA11233 3
 [2,]   NA   NA   NA   NA11233 3
 [3,]   NA   NA   NA   NA11233 3
 [4,]   NA   NA   NA   NA11233 3
 [5,]1111   NA   NA655 5
 [6,]1111   NA   NA655 5
 [7,]222266   NA44 4
 [8,]3333554   NA   NANA
 [9,]3333554   NA   NANA
[10,]3333554   NA   NANA

I use the following code:

M <- matrix(NA, 10, 10)

for (i in 1:10) {
   for (j in 1:10) {
  if (i %in% 1:4 & j %in% 5:6) M[i,j] <- M[j,i] <- m[1,2]
  if (i %in% 1:4 & j == 7) M[i,j] <- M[j,i] <-m[1,3]
  if (i %in% 1:4 & j %in% 8:10) M[i,j] <- M[j,i] <-m[1,4]
  if (i %in% 5:6 & j == 7) M[i,j] <- M[j,i] <-m[2,3]
  if (i %in% 5:6 & j %in% 8:10) M[i,j] <- M[j,i] <-m[2,4]
  if (i == 7 & j %in% 8:10) M[i,j] <- M[j,i] <-m[3,4]
   }
}

Is there any convenience way to do it? Thanks!

Best,
Jinsong

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


Re: [R] expand gridded matrix to higher resolution

2017-07-06 Thread Anthoni, Peter (IMK)
Hi Jeff,

thanks, the raster package disaggregate will do the trick as well.

library(raster)
rmm <- raster(ncols=5, nrows=3)
rmm[] <- matrix(1:15,nrow=3,byrow = T)
xrmm <- disaggregate(rmm, fact=c(3, 3))
> > as.matrix(rmm)
>  [,1] [,2] [,3] [,4] [,5]
> [1,]12345
> [2,]6789   10
> [3,]   11   12   13   14   15
> > as.matrix(xrmm)
>   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] 
> [,14] [,15]
>  [1,]111222333 4 4 4 5
>  5 5
>  [2,]111222333 4 4 4 5
>  5 5
>  [3,]111222333 4 4 4 5
>  5 5
>  [4,]666777888 9 9 910
> 1010
>  [5,]666777888 9 9 910
> 1010
>  [6,]666777888 9 9 910
> 1010
>  [7,]   11   11   11   12   12   12   13   13   1314141415
> 1515
>  [8,]   11   11   11   12   12   12   13   13   1314141415
> 1515
>  [9,]   11   11   11   12   12   12   13   13   1314141415
> 1515


the disaggregate as a bit faster than the tapply.

mmb=matrix(1:259200,nrow=720,ncol=360)
rmmb <- raster(ncols=360, nrows=720)
rmmb[] <- mmb[]
system.time(for(i in 1:10) {xmm=matrix(NA,nrow=nrow(mmb)*3,ncol=ncol(mmb)*3)
for(icol in 1:ncol(mmb)) {
  for(irow in 1:nrow(mmb)) {
xicol=(icol-1)*3 +c(1:3)
xirow=(irow-1)*3 +c(1:3)
xmm[xirow,xicol]=mmb[irow,icol]
  }
}
})
system.time(for(i in 1:10) {apply(t(apply(mmb,1,rep,each=3)),2,rep,each=3)}) 
#ca. 10x faster
system.time(for(i in 1:10) {xrmmb <- disaggregate(rmmb, fact=c(3, 3))}) 

> > system.time(for(i in 1:10) {xmm=matrix(NA,nrow=nrow(mmb)*3,ncol=ncol(mmb)*3)
> + for(icol in 1:ncol(mmb)) {
> +   for(irow in 1:nrow(mmb)) {
> + xicol=(icol-1)*3 +c(1:3)
> + xirow=(irow-1)*3 +c(1:3)
> + xmm[xirow,xicol]=mmb[irow,icol]
> +   }
> + }
> + })
>user  system elapsed 
>   8.297   0.048   8.364 
> > system.time(for(i in 1:10) 
> > {apply(t(apply(mmb,1,rep,each=3)),2,rep,each=3)}) #ca. 10x faster
>user  system elapsed 
>   0.785   0.093   0.881 
> > system.time(for(i in 1:10) {xrmmb <- disaggregate(rmmb, fact=c(3, 3))})
>user  system elapsed 
>   0.583   0.147   0.731 

cheers
Peter



> On 5. Jul 2017, at 16:57, Jeff Newmiller  wrote:
> 
> You probably ought to be using the raster package. See the CRAN Spatial Task 
> View.
> -- 
> Sent from my phone. Please excuse my brevity.
> 
> On July 5, 2017 12:20:28 AM PDT, "Anthoni, Peter (IMK)" 
>  wrote:
>> Hi all,
>> (if me email goes out as html, than my email client don't do as told,
>> and I apologies already.)
>> 
>> We need to downscale climate data and therefore first need to expand
>> the climate from 0.5deg to the higher resolution 10min, before we can
>> add high resolution deviations. We basically need to have the original
>> data at each gridcell replicated into 3x3 gridcells. 
>> A simple for loop can do this, but I could need a faster procedure.
>> Anybody know a faster way? Is there package than can do what we need
>> already?
>> I tried matrix with rep, but I am missing some magic there, since it
>> doesn't do what we need. 
>> replicate might be promising, but then still need to rearrange the
>> output into the column and row format we need. 
>> 
>> A simple example:
>> mm=matrix(1:15,nrow=3,byrow = T)
>> xmm=matrix(NA,nrow=nrow(mm)*3,ncol=ncol(mm)*3)
>> for(icol in 1:ncol(mm)) {
>> for(irow in 1:nrow(mm)) {
>>   xicol=(icol-1)*3 +c(1:3)
>>   xirow=(irow-1)*3 +c(1:3)
>>   xmm[xirow,xicol]=mm[irow,icol]
>> }
>> }
>> mm
 mm
>>> [,1] [,2] [,3] [,4] [,5]
>>> [1,]12345
>>> [2,]6789   10
>>> [3,]   11   12   13   14   15
>>> 
>> xmm
 xmm
>>>  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
>> [,13] [,14] [,15]
>>> [1,]111222333 4 4 4 
>>  5 5 5
>>> [2,]111222333 4 4 4 
>>  5 5 5
>>> [3,]111222333 4 4 4 
>>  5 5 5
>>> [4,]666777888 9 9 9 
>> 101010
>>> [5,]666777888 9 9 9 
>> 101010
>>> [6,]666777888 9 9 9 
>> 101010
>>> [7,]   11   11   11   12   12   12   13   13   13141414 
>> 151515
>>> [8,]   11   11   11   12   12   12   13   13   13141414 
>> 151515
>>> [9,]   11   11   11   12   12   12   13   13   13141414 
>> 151515
>> 
>> I tried various rep with matrix, but don't get the right result.
>> 

Re: [R] expand gridded matrix to higher resolution

2017-07-05 Thread Jeff Newmiller
You probably ought to be using the raster package. See the CRAN Spatial Task 
View.
-- 
Sent from my phone. Please excuse my brevity.

On July 5, 2017 12:20:28 AM PDT, "Anthoni, Peter (IMK)"  
wrote:
>Hi all,
>(if me email goes out as html, than my email client don't do as told,
>and I apologies already.)
>
>We need to downscale climate data and therefore first need to expand
>the climate from 0.5deg to the higher resolution 10min, before we can
>add high resolution deviations. We basically need to have the original
>data at each gridcell replicated into 3x3 gridcells. 
>A simple for loop can do this, but I could need a faster procedure.
>Anybody know a faster way? Is there package than can do what we need
>already?
>I tried matrix with rep, but I am missing some magic there, since it
>doesn't do what we need. 
>replicate might be promising, but then still need to rearrange the
>output into the column and row format we need. 
>
>A simple example:
>mm=matrix(1:15,nrow=3,byrow = T)
>xmm=matrix(NA,nrow=nrow(mm)*3,ncol=ncol(mm)*3)
>for(icol in 1:ncol(mm)) {
>  for(irow in 1:nrow(mm)) {
>xicol=(icol-1)*3 +c(1:3)
>xirow=(irow-1)*3 +c(1:3)
>xmm[xirow,xicol]=mm[irow,icol]
>  }
>}
>mm
>> > mm
>>  [,1] [,2] [,3] [,4] [,5]
>> [1,]12345
>> [2,]6789   10
>> [3,]   11   12   13   14   15
>> 
>xmm
>> > xmm
>>   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
>[,13] [,14] [,15]
>>  [1,]111222333 4 4 4 
>   5 5 5
>>  [2,]111222333 4 4 4 
>   5 5 5
>>  [3,]111222333 4 4 4 
>   5 5 5
>>  [4,]666777888 9 9 9 
>  101010
>>  [5,]666777888 9 9 9 
>  101010
>>  [6,]666777888 9 9 9 
>  101010
>>  [7,]   11   11   11   12   12   12   13   13   13141414 
>  151515
>>  [8,]   11   11   11   12   12   12   13   13   13141414 
>  151515
>>  [9,]   11   11   11   12   12   12   13   13   13141414 
>  151515
>
>I tried various rep with matrix, but don't get the right result.
>xmm2=matrix(rep(rep(mm,each=3),times=3),nrow=nrow(mm)*3,ncol=ncol(mm)*3,byrow
>= F)
>> identical(xmm,xmm2)
>[1] FALSE
>
>rr=replicate(3,rep(t(mm),each=3))
>rr
>> > rr
>>   [,1] [,2] [,3]
>>  [1,]111
>>  [2,]111
>>  [3,]111
>>  [4,]222
>>  [5,]222
>>  [6,]222
>>  [7,]333
>> ...
>identical(xmm,matrix(rr,ncol=15,nrow=9,byrow=T))
>> > identical(xmm,matrix(rr,ncol=15,nrow=9,byrow=T))
>> [1] FALSE
> 
>Many thanks for any advice.
>
>cheers
>Peter
>
>__
>R-help@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.

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


Re: [R] expand gridded matrix to higher resolution

2017-07-05 Thread Anthoni, Peter (IMK)
Hi Jim,

thanks that works like a charm.  

cheers
Peter



> On 5. Jul 2017, at 12:01, Jim Lemon  wrote:
> 
> Hi Peter,
> 
> apply(t(apply(mm,1,rep,each=3)),2,rep,each=3)
> 
> Jim
> 
> On Wed, Jul 5, 2017 at 5:20 PM, Anthoni, Peter (IMK)
>  wrote:
>> Hi all,
>> (if me email goes out as html, than my email client don't do as told, and I 
>> apologies already.)
>> 
>> We need to downscale climate data and therefore first need to expand the 
>> climate from 0.5deg to the higher resolution 10min, before we can add high 
>> resolution deviations. We basically need to have the original data at each 
>> gridcell replicated into 3x3 gridcells.
>> A simple for loop can do this, but I could need a faster procedure. Anybody 
>> know a faster way? Is there package than can do what we need already?
>> I tried matrix with rep, but I am missing some magic there, since it doesn't 
>> do what we need.
>> replicate might be promising, but then still need to rearrange the output 
>> into the column and row format we need.
>> 
>> A simple example:
>> mm=matrix(1:15,nrow=3,byrow = T)
>> xmm=matrix(NA,nrow=nrow(mm)*3,ncol=ncol(mm)*3)
>> for(icol in 1:ncol(mm)) {
>>  for(irow in 1:nrow(mm)) {
>>xicol=(icol-1)*3 +c(1:3)
>>xirow=(irow-1)*3 +c(1:3)
>>xmm[xirow,xicol]=mm[irow,icol]
>>  }
>> }
>> mm
 mm
>>> [,1] [,2] [,3] [,4] [,5]
>>> [1,]12345
>>> [2,]6789   10
>>> [3,]   11   12   13   14   15
>>> 
>> xmm
 xmm
>>>  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] 
>>> [,14] [,15]
>>> [1,]111222333 4 4 4 5   
>>>   5 5
>>> [2,]111222333 4 4 4 5   
>>>   5 5
>>> [3,]111222333 4 4 4 5   
>>>   5 5
>>> [4,]666777888 9 9 910   
>>>  1010
>>> [5,]666777888 9 9 910   
>>>  1010
>>> [6,]666777888 9 9 910   
>>>  1010
>>> [7,]   11   11   11   12   12   12   13   13   1314141415   
>>>  1515
>>> [8,]   11   11   11   12   12   12   13   13   1314141415   
>>>  1515
>>> [9,]   11   11   11   12   12   12   13   13   1314141415   
>>>  1515
>> 
>> I tried various rep with matrix, but don't get the right result.
>> xmm2=matrix(rep(rep(mm,each=3),times=3),nrow=nrow(mm)*3,ncol=ncol(mm)*3,byrow
>>  = F)
>>> identical(xmm,xmm2)
>> [1] FALSE
>> 
>> rr=replicate(3,rep(t(mm),each=3))
>> rr
 rr
>>>  [,1] [,2] [,3]
>>> [1,]111
>>> [2,]111
>>> [3,]111
>>> [4,]222
>>> [5,]222
>>> [6,]222
>>> [7,]333
>>> ...
>> identical(xmm,matrix(rr,ncol=15,nrow=9,byrow=T))
 identical(xmm,matrix(rr,ncol=15,nrow=9,byrow=T))
>>> [1] FALSE
>> 
>> Many thanks for any advice.
>> 
>> cheers
>> Peter
>> 
>> __
>> R-help@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.

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


Re: [R] expand gridded matrix to higher resolution

2017-07-05 Thread Jim Lemon
Hi Peter,

apply(t(apply(mm,1,rep,each=3)),2,rep,each=3)

Jim

On Wed, Jul 5, 2017 at 5:20 PM, Anthoni, Peter (IMK)
 wrote:
> Hi all,
> (if me email goes out as html, than my email client don't do as told, and I 
> apologies already.)
>
> We need to downscale climate data and therefore first need to expand the 
> climate from 0.5deg to the higher resolution 10min, before we can add high 
> resolution deviations. We basically need to have the original data at each 
> gridcell replicated into 3x3 gridcells.
> A simple for loop can do this, but I could need a faster procedure. Anybody 
> know a faster way? Is there package than can do what we need already?
> I tried matrix with rep, but I am missing some magic there, since it doesn't 
> do what we need.
> replicate might be promising, but then still need to rearrange the output 
> into the column and row format we need.
>
> A simple example:
> mm=matrix(1:15,nrow=3,byrow = T)
> xmm=matrix(NA,nrow=nrow(mm)*3,ncol=ncol(mm)*3)
> for(icol in 1:ncol(mm)) {
>   for(irow in 1:nrow(mm)) {
> xicol=(icol-1)*3 +c(1:3)
> xirow=(irow-1)*3 +c(1:3)
> xmm[xirow,xicol]=mm[irow,icol]
>   }
> }
> mm
>> > mm
>>  [,1] [,2] [,3] [,4] [,5]
>> [1,]12345
>> [2,]6789   10
>> [3,]   11   12   13   14   15
>>
> xmm
>> > xmm
>>   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] 
>> [,14] [,15]
>>  [1,]111222333 4 4 4 5   
>>   5 5
>>  [2,]111222333 4 4 4 5   
>>   5 5
>>  [3,]111222333 4 4 4 5   
>>   5 5
>>  [4,]666777888 9 9 910   
>>  1010
>>  [5,]666777888 9 9 910   
>>  1010
>>  [6,]666777888 9 9 910   
>>  1010
>>  [7,]   11   11   11   12   12   12   13   13   1314141415   
>>  1515
>>  [8,]   11   11   11   12   12   12   13   13   1314141415   
>>  1515
>>  [9,]   11   11   11   12   12   12   13   13   1314141415   
>>  1515
>
> I tried various rep with matrix, but don't get the right result.
> xmm2=matrix(rep(rep(mm,each=3),times=3),nrow=nrow(mm)*3,ncol=ncol(mm)*3,byrow 
> = F)
>> identical(xmm,xmm2)
> [1] FALSE
>
> rr=replicate(3,rep(t(mm),each=3))
> rr
>> > rr
>>   [,1] [,2] [,3]
>>  [1,]111
>>  [2,]111
>>  [3,]111
>>  [4,]222
>>  [5,]222
>>  [6,]222
>>  [7,]333
>> ...
> identical(xmm,matrix(rr,ncol=15,nrow=9,byrow=T))
>> > identical(xmm,matrix(rr,ncol=15,nrow=9,byrow=T))
>> [1] FALSE
>
> Many thanks for any advice.
>
> cheers
> Peter
>
> __
> R-help@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.

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


[R] expand gridded matrix to higher resolution

2017-07-05 Thread Anthoni, Peter (IMK)
Hi all,
(if me email goes out as html, than my email client don't do as told, and I 
apologies already.)

We need to downscale climate data and therefore first need to expand the 
climate from 0.5deg to the higher resolution 10min, before we can add high 
resolution deviations. We basically need to have the original data at each 
gridcell replicated into 3x3 gridcells. 
A simple for loop can do this, but I could need a faster procedure. Anybody 
know a faster way? Is there package than can do what we need already?
I tried matrix with rep, but I am missing some magic there, since it doesn't do 
what we need. 
replicate might be promising, but then still need to rearrange the output into 
the column and row format we need. 

A simple example:
mm=matrix(1:15,nrow=3,byrow = T)
xmm=matrix(NA,nrow=nrow(mm)*3,ncol=ncol(mm)*3)
for(icol in 1:ncol(mm)) {
  for(irow in 1:nrow(mm)) {
xicol=(icol-1)*3 +c(1:3)
xirow=(irow-1)*3 +c(1:3)
xmm[xirow,xicol]=mm[irow,icol]
  }
}
mm
> > mm
>  [,1] [,2] [,3] [,4] [,5]
> [1,]12345
> [2,]6789   10
> [3,]   11   12   13   14   15
> 
xmm
> > xmm
>   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] 
> [,14] [,15]
>  [1,]111222333 4 4 4 5
>  5 5
>  [2,]111222333 4 4 4 5
>  5 5
>  [3,]111222333 4 4 4 5
>  5 5
>  [4,]666777888 9 9 910
> 1010
>  [5,]666777888 9 9 910
> 1010
>  [6,]666777888 9 9 910
> 1010
>  [7,]   11   11   11   12   12   12   13   13   1314141415
> 1515
>  [8,]   11   11   11   12   12   12   13   13   1314141415
> 1515
>  [9,]   11   11   11   12   12   12   13   13   1314141415
> 1515

I tried various rep with matrix, but don't get the right result.
xmm2=matrix(rep(rep(mm,each=3),times=3),nrow=nrow(mm)*3,ncol=ncol(mm)*3,byrow = 
F)
> identical(xmm,xmm2)
[1] FALSE

rr=replicate(3,rep(t(mm),each=3))
rr
> > rr
>   [,1] [,2] [,3]
>  [1,]111
>  [2,]111
>  [3,]111
>  [4,]222
>  [5,]222
>  [6,]222
>  [7,]333
> ...
identical(xmm,matrix(rr,ncol=15,nrow=9,byrow=T))
> > identical(xmm,matrix(rr,ncol=15,nrow=9,byrow=T))
> [1] FALSE
 
Many thanks for any advice.

cheers
Peter

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