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

2006-07-27 Thread jim holtman
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   8191
 [2,]2   12   22   32   42   52   62   72   8292
 [3,]3   13   23   33   43   53   63   73   8393
 [4,]4   14   24   34   44   54   64   74   8494
 [5,]5   15   25   35   45   55   65   75   8595
 [6,]6   16   26   36   46   56   66   76   8696
 [7,]7   17   27   37   47   57   67   77   8797
 [8,]8   18   28   38   48   58   68   78   8898
 [9,]9   19   29   39   49   59   69   79   8999
[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 [EMAIL PROTECTED] 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,
 Vladimirmailto:[EMAIL PROTECTED]

 __
 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]]

__
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.


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

2006-07-27 Thread Robin Hankin
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   8191
  [2,]2   12   22   32   42   52   62   72   8292
  [3,]3   13   23   33   43   53   63   73   8393
  [4,]4   14   24   34   44   54   64   74   8494
  [5,]5   15   25   35   45   55   65   75   8595
  [6,]6   16   26   36   46   56   66   76   8696
  [7,]7   17   27   37   47   57   67   77   8797
  [8,]8   18   28   38   48   58   68   78   8898
  [9,]9   19   29   39   49   59   69   79   8999
 [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 [EMAIL PROTECTED] 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,
 Vladimirmailto:[EMAIL PROTECTED]

 __
 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]]

 __
 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.

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

__
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.


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

2006-07-27 Thread Gabor Grothendieck
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 [EMAIL PROTECTED] 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,
 Vladimirmailto:[EMAIL PROTECTED]

 __
 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.


__
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.


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

2006-07-27 Thread Vladimir Eremeev
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 [EMAIL PROTECTED] 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 Vladimirmailto:[EMAIL PROTECTED]

jh __
jh R-help@stat.math.ethz.ch mailing list
jh https://stat.ethz.ch/mailman/listinfo/r-help
jh PLEASE do read the posting guide
jh http://www.R-project.org/posting-guide.html
jh and provide commented, minimal, self-contained, reproducible code.










---
Best regards,
Vladimirmailto:[EMAIL PROTECTED]
==
Research Scientist, PhD   Leninsky Prospect 33,
Space Monitoring  Ecoinformation Systems Sector, Moscow, Russia, 119071,
Institute of Ecology, Phone: (095) 135-9972;
Russian Academy of Sciences   Fax: (095) 135-9972

__
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.


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

2006-07-27 Thread Vladimir Eremeev
Dear Robin,

Thank you, seems it is what I need.

---
Best regards,
Vladimirmailto:[EMAIL PROTECTED]

__
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.


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

2006-07-27 Thread Vladimir Eremeev
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

__
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.