# [R] how to resample (or resize) matrix?

Thu Jul 27 13:15:25 CEST 2006

```Dear jim,

Yes.
But, unfortunately, two nested for loops will execute very slow.

It is not very serious problem to do my task with an image
processing package, I am wondering if it is efficiently possible with
R.

Thursday, July 27, 2006, 2:20:13 PM, you wrote:

jh> Is this what you want: the mean of the surrounding 4 cells?

jh>

>> x <- matrix(1:100, 10)  # create data
>> rmean <- matrix(0,5,5)  # result matrix
>> for (i in 1:5){
jh> +     for (j in 1:5){
jh> +         rmean[i, j] <- mean(x[c(-1,0) + 2 * i, c(-1,0) + 2 * j])
jh> +     }
jh> + }
>> x
jh>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
jh>  [1,]    1   11   21   31   41   51   61   71   81    91
jh>  [2,]    2   12   22   32   42   52   62   72   82    92
jh>  [3,]    3   13   23   33   43   53   63   73   83    93
jh>  [4,]    4   14   24   34   44   54   64   74   84    94
jh>  [5,]    5   15   25   35   45   55   65   75   85    95
jh>  [6,]    6   16   26   36   46   56   66   76   86    96
jh>  [7,]    7   17   27   37   47   57   67   77   87    97
jh>  [8,]    8   18   28   38   48   58   68   78   88    98
jh>  [9,]    9   19   29   39   49   59   69   79   89    99
jh> [10,]   10   20   30   40   50   60   70   80   90   100
>> rmean
jh>      [,1] [,2] [,3] [,4] [,5]
jh> [1,]  6.5 26.5 46.5 66.5 86.5
jh> [2,]  8.5 28.5 48.5 68.5 88.5
jh> [3,] 10.5 30.5 50.5 70.5 90.5
jh> [4,] 12.5 32.5 52.5 72.5 92.5
jh> [5,] 14.5 34.5 54.5 74.5 94.5
>>

jh>

jh> On 7/27/06, Vladimir Eremeev <wl at eimb.ru> wrote:Dear r-help,

jh> I have a matrix, suppose, 10x10, and I need the matrix 5x5, having
jh> in each cell a mean value of the cells from the initial matrix.

jh> Please, point me to a function in R, which can help me doing that.

jh> Digging the documentation and mail archives didn't give me a result.

jh> Thank you.

jh> ---
jh> Best regards,

jh> ______________________________________________
jh> R-help at stat.math.ethz.ch mailing list
jh> https://stat.ethz.ch/mailman/listinfo/r-help
jh> http://www.R-project.org/posting-guide.html
jh> and provide commented, minimal, self-contained, reproducible code.

---
Best regards,