Re: [R] analog to the matlab buffer function?

2006-12-01 Thread Robin Hankin
  do.call(rbind,lapply(0:4,function(i){i+(1:5)}))
  [,1] [,2] [,3] [,4] [,5]
[1,]12345
[2,]23456
[3,]34567
[4,]45678
[5,]56789
 



On 1 Dec 2006, at 00:32, Charles C. Berry wrote:


 See

   ?embed

 It is not quite the same, but this seems to be what you want - at  
 least
 for the example you give:

 t( embed(1:5,3) )[3:1,]
   [,1] [,2] [,3]
 [1,]123
 [2,]234
 [3,]345


 On Fri, 1 Dec 2006, Martin Ivanov wrote:

 Hello! I am new to R. I could not find a function analogous to  
 matlab's
 function buffer, which is used in signal processing. Is there such a
 function in R? What I need to do is as follows. If I apply the  
 function
 to the vector c(1:5) for example with a window length 3 and  
 overlapping
 2, I need to get a matrix like this:
 1 2 3
 2 3 4
 3 4 5
 In matlab this is achieved with the function buffer. Is there  
 ananalogous R function?

 Thank you very much in advance.
 Regards,
 Martin

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


 Charles C. Berry(858) 534-2098
   Dept of Family/Preventive  
 Medicine
 E mailto:[EMAIL PROTECTED] UC San Diego
 http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego  
 92093-0717

 __
 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] analog to the matlab buffer function?

2006-12-01 Thread Gregory R. Warnes
The gtools package also includes a function, 'running', which accomplishes
the desired task:

 library(gtools)
 t(running(1:5, width=3, fun=c))
[,1] [,2] [,3]
1:3123
2:4234
3:5345
  


On 11/30/06 8:56 PM, Marc Schwartz [EMAIL PROTECTED] wrote:

 Here is another possibility, though I may be missing how the Matlab
 function handles incomplete rows generated at the end of the source
 vector. I have not fully tested this, so it may yet require some
 tweaking and certainly appropriate error checking.
 
 I am presuming that the basic premise is that each row is of length
 'window' and that it overlaps with the END of prior row by 'overlap'.
 
 
 Buffer - function(x, window, overlap)
 {
   Res - NULL
 
   while (length(x) = window)
   {
 Res - c(Res, x[1:window])
 x - x[(1 + window - overlap):length(x)]
   }
 
   matrix(Res, ncol = window, byrow = TRUE)
 }
 
 
 Buffer(1:5, 3, 2)
  [,1] [,2] [,3]
 [1,]123
 [2,]234
 [3,]345
 
 Buffer(1:10, 4, 2)
  [,1] [,2] [,3] [,4]
 [1,]1234
 [2,]3456
 [3,]5678
 [4,]789   10
 
 HTH,
 
 Marc Schwartz
 
 
 On Thu, 2006-11-30 at 16:32 -0800, Charles C. Berry wrote:
 See
 
 ?embed
 
 It is not quite the same, but this seems to be what you want - at least
 for the example you give:
 
 t( embed(1:5,3) )[3:1,]
   [,1] [,2] [,3]
 [1,]123
 [2,]234
 [3,]345
 
 
 On Fri, 1 Dec 2006, Martin Ivanov wrote:
 
 Hello! I am new to R. I could not find a function analogous to matlab's
 function buffer, which is used in signal processing. Is there such a
 function in R? What I need to do is as follows. If I apply the function
 to the vector c(1:5) for example with a window length 3 and overlapping
 2, I need to get a matrix like this:
 1 2 3
 2 3 4
 3 4 5
 In matlab this is achieved with the function buffer. Is there ananalogous R
 function?
 
 __
 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] analog to the matlab buffer function?

2006-11-30 Thread Charles C. Berry

See

?embed

It is not quite the same, but this seems to be what you want - at least 
for the example you give:

 t( embed(1:5,3) )[3:1,]
  [,1] [,2] [,3]
[1,]123
[2,]234
[3,]345


On Fri, 1 Dec 2006, Martin Ivanov wrote:

 Hello! I am new to R. I could not find a function analogous to matlab's 
 function buffer, which is used in signal processing. Is there such a 
 function in R? What I need to do is as follows. If I apply the function 
 to the vector c(1:5) for example with a window length 3 and overlapping 
 2, I need to get a matrix like this:
 1 2 3
 2 3 4
 3 4 5
 In matlab this is achieved with the function buffer. Is there ananalogous R 
 function?

 Thank you very much in advance.
 Regards,
 Martin

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


Charles C. Berry(858) 534-2098
  Dept of Family/Preventive Medicine
E mailto:[EMAIL PROTECTED]   UC San Diego
http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0717

__
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] analog to the matlab buffer function?

2006-11-30 Thread Marc Schwartz
Here is another possibility, though I may be missing how the Matlab
function handles incomplete rows generated at the end of the source
vector. I have not fully tested this, so it may yet require some
tweaking and certainly appropriate error checking.

I am presuming that the basic premise is that each row is of length
'window' and that it overlaps with the END of prior row by 'overlap'. 


Buffer - function(x, window, overlap)
{
  Res - NULL

  while (length(x) = window)
  {
Res - c(Res, x[1:window])
x - x[(1 + window - overlap):length(x)]
  }

  matrix(Res, ncol = window, byrow = TRUE)
}


 Buffer(1:5, 3, 2)
 [,1] [,2] [,3]
[1,]123
[2,]234
[3,]345

 Buffer(1:10, 4, 2)
 [,1] [,2] [,3] [,4]
[1,]1234
[2,]3456
[3,]5678
[4,]789   10

HTH,

Marc Schwartz


On Thu, 2006-11-30 at 16:32 -0800, Charles C. Berry wrote: 
 See
 
   ?embed
 
 It is not quite the same, but this seems to be what you want - at least 
 for the example you give:
 
  t( embed(1:5,3) )[3:1,]
   [,1] [,2] [,3]
 [1,]123
 [2,]234
 [3,]345
 
 
 On Fri, 1 Dec 2006, Martin Ivanov wrote:
 
  Hello! I am new to R. I could not find a function analogous to matlab's 
  function buffer, which is used in signal processing. Is there such a 
  function in R? What I need to do is as follows. If I apply the function 
  to the vector c(1:5) for example with a window length 3 and overlapping 
  2, I need to get a matrix like this:
  1 2 3
  2 3 4
  3 4 5
  In matlab this is achieved with the function buffer. Is there ananalogous R 
  function?

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