[R] how to resample (or resize) matrix?
Robin Hankin
r.hankin at noc.soton.ac.uk
Thu Jul 27 12:42:51 CEST 2006
Right, I think I understand the question.
library(magic)
?subsums
If you want a windowed moving average, do:
x <- matrix(1:100,10,10)
subsums(x,6,FUN="mean",pad=NA,wrap=F)[6:10,6:10]
If you want block average, do:
subsums(x,2,FUN="mean",pad=NA,wrap=F)[seq(2,10,2),seq(2,10,2)]
which agrees with Jim's solution below.
HTH
rksh
On 27 Jul 2006, at 11:20, jim holtman wrote:
> Is this what you want: the mean of the surrounding 4 cells?
>
>> x <- matrix(1:100, 10) # create data
>> rmean <- matrix(0,5,5) # result matrix
>> for (i in 1:5){
> + for (j in 1:5){
> + rmean[i, j] <- mean(x[c(-1,0) + 2 * i, c(-1,0) + 2 * j])
> + }
> + }
>> x
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,] 1 11 21 31 41 51 61 71 81 91
> [2,] 2 12 22 32 42 52 62 72 82 92
> [3,] 3 13 23 33 43 53 63 73 83 93
> [4,] 4 14 24 34 44 54 64 74 84 94
> [5,] 5 15 25 35 45 55 65 75 85 95
> [6,] 6 16 26 36 46 56 66 76 86 96
> [7,] 7 17 27 37 47 57 67 77 87 97
> [8,] 8 18 28 38 48 58 68 78 88 98
> [9,] 9 19 29 39 49 59 69 79 89 99
> [10,] 10 20 30 40 50 60 70 80 90 100
>> rmean
> [,1] [,2] [,3] [,4] [,5]
> [1,] 6.5 26.5 46.5 66.5 86.5
> [2,] 8.5 28.5 48.5 68.5 88.5
> [3,] 10.5 30.5 50.5 70.5 90.5
> [4,] 12.5 32.5 52.5 72.5 92.5
> [5,] 14.5 34.5 54.5 74.5 94.5
>>
>
>
>
> On 7/27/06, Vladimir Eremeev <wl at eimb.ru> wrote:
>>
>> Dear r-help,
>>
>> I have a matrix, suppose, 10x10, and I need the matrix 5x5, having
>> in each cell a mean value of the cells from the initial matrix.
>>
>> Please, point me to a function in R, which can help me doing that.
>>
>> Digging the documentation and mail archives didn't give me a result.
>>
>> Thank you.
>>
>> ---
>> Best regards,
>> Vladimir mailto:wl at eimb.ru
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
More information about the R-help
mailing list