# [R] expand gridded matrix to higher resolution

Jim Lemon drjimlemon at gmail.com
Wed Jul 5 12:01:25 CEST 2017

```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)
<peter.anthoni at kit.edu> 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,]    1    2    3    4    5
>> [2,]    6    7    8    9   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,]    1    1    1    2    2    2    3    3    3     4     4     4     5     5     5
>>  [2,]    1    1    1    2    2    2    3    3    3     4     4     4     5     5     5
>>  [3,]    1    1    1    2    2    2    3    3    3     4     4     4     5     5     5
>>  [4,]    6    6    6    7    7    7    8    8    8     9     9     9    10    10    10
>>  [5,]    6    6    6    7    7    7    8    8    8     9     9     9    10    10    10
>>  [6,]    6    6    6    7    7    7    8    8    8     9     9     9    10    10    10
>>  [7,]   11   11   11   12   12   12   13   13   13    14    14    14    15    15    15
>>  [8,]   11   11   11   12   12   12   13   13   13    14    14    14    15    15    15
>>  [9,]   11   11   11   12   12   12   13   13   13    14    14    14    15    15    15
>
> 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,]    1    1    1
>>  [2,]    1    1    1
>>  [3,]    1    1    1
>>  [4,]    2    2    2
>>  [5,]    2    2    2
>>  [6,]    2    2    2
>>  [7,]    3    3    3
>> ...
> 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 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.

```