Hi, I would like to know if there is a way to change a vector of arbitrary size to make it fits the nearest upper size multiple of a power of 2. -- Cordialy ---------------------------------------- Emmanuel POIZOT Cnam/Intechmer Digue de Collignon 50110 Tourlaville T?l : (33)(0)2 33 88 73 42 Fax : (33)(0)2 33 88 73 39 -----------------------------------------
Is the following what you want?> x<- rnorm(800) > xt <- x[1:2^trunc(log(length(x),base=2))] > length(xt)[1] 512 HTH, Andy> -----Original Message----- > From: Poizot Emmanuel [mailto:poizot at cnam.fr] > Sent: Friday, February 14, 2003 4:52 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Change array size > > > Hi, > I would like to know if there is a way to change a vector of > arbitrary size > to make it fits the nearest upper size multiple of a power of 2. > > -- > Cordialy > ---------------------------------------- > Emmanuel POIZOT > Cnam/Intechmer > Digue de Collignon > 50110 Tourlaville > T?l : (33)(0)2 33 88 73 42 > Fax : (33)(0)2 33 88 73 39 > ----------------------------------------- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help >------------------------------------------------------------------------------
If you just want to extend the vector, and fill new elements with NA, then a simple way is: > x <- 1:5 > length(x) <- 2 ^ ceiling(log(length(x))/log(2)) > x [1] 1 2 3 4 5 NA NA NA > However, one needs to be careful with kind of arithmetic -- floating point inaccuracies can cause unexpected results. E.g., one would expect the following points to form a straight line, but they don't -- on my computer there are 4 anomalies between 29 and 50: > plot(ceiling(log(2**(1:50))/log(2))) In the question asked, this is unlikely to cause problems as it is unlikely that a vector will have 2^29 elements. At Friday 09:51 AM 2/14/2003 +0000, Poizot Emmanuel wrote:>Hi, >I would like to know if there is a way to change a vector of arbitrary size >to make it fits the nearest upper size multiple of a power of 2. > >-- >Cordialy >---------------------------------------- >Emmanuel POIZOT >Cnam/Intechmer >Digue de Collignon >50110 Tourlaville >T?l : (33)(0)2 33 88 73 42 >Fax : (33)(0)2 33 88 73 39 >----------------------------------------- > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >http://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Fri, Feb 14, 2003 at 09:51:38AM +0000, Poizot Emmanuel wrote:> Hi, > I would like to know if there is a way to change a vector of arbitrary size > to make it fits the nearest upper size multiple of a power of 2.Do you want to interpolate to a new shape, or zero-pad? If zero-padding is what you're after, I think nextn() is what you want...> zz <- rnorm(10) > zz.pad <- vector(length=nextn(length(zz),factors=2)) > zz.pad[1:length(zz)] <- zz > zz.pad[1] 0.2759134 -1.3738876 0.9816857 -0.4639678 -1.0549718 2.8562435 [7] -0.6275852 0.1169049 0.2463690 0.9059579 0.0000000 0.0000000 [13] 0.0000000 0.0000000 0.0000000 0.0000000 Cheers Jason -- Indigo Industrial Controls Ltd. 64-21-343-545 jasont at indigoindustrial.co.nz