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
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@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@eimb.ru > > ______________________________________________ > R-help@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]]
Assuming the problem is to partition the 10x10 matrix x into 25 two by two squares and then average each of those squares, try this: apply(array(x, c(2,5,2,5)), c(2,4), mean) 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. >
Dear Sean, Thursday, July 27, 2006, 3:31:31 PM, you wrote: SOR> Hi Vladimir, SOR> I was wondering whether this was image related :-) Yes, that's right, I am doing image processing with R. SOR> would one of the image related libraries do it for you? SOR> looking at SOR> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Add_002don-packages-from-CRAN SOR> and searching down for "image" The EBImage seems to be able. But I need windows binaries, which are unavailable. SOR> cheers, SOR> Sean