I need an efficient way to build a new n x (n-1)/2 vector from an n-vector x as: c(x[-1]-x[1], x[-(1:2)]-x[2], ... , x[-(1:(n-1)] - x[n-1]) x is increasing with x[1] = 0. The following works but is not the greatest: junk<-outer(x, x, '-') junk[junk>0] e.g., given x<-c(0, 3, 7, 20) junk<-outer(x, x, '-') junk[junk>0] # yields: c(3, 7, 20, 4, 17, 13) as needed, but it has to go through junk # [,1] [,2] [,3] [,4] #[1,] 0 -3 -7 -20 #[2,] 3 0 -4 -17 #[3,] 7 4 0 -13 #[4,] 20 17 13 0 Anyone have a better idea? -Dan -- View this message in context: http://r.789695.n4.nabble.com/vector-manipulations-differences-tp4712575.html Sent from the R help mailing list archive at Nabble.com.
Use ?mappy and ?rep.int> x[unlist(mapply(":",2:4,4))] - x[rep.int(1:3,3:1)][1] 3 7 20 4 17 13 Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Mon, Sep 21, 2015 at 2:17 PM, Dan D <ddalthorp at usgs.gov> wrote:> I need an efficient way to build a new n x (n-1)/2 vector from an n-vector x > as: > > c(x[-1]-x[1], x[-(1:2)]-x[2], ... , x[-(1:(n-1)] - x[n-1]) > > x is increasing with x[1] = 0. > > The following works but is not the greatest: > junk<-outer(x, x, '-') > junk[junk>0] > > e.g., > given > x<-c(0, 3, 7, 20) > junk<-outer(x, x, '-') > junk[junk>0] # yields: c(3, 7, 20, 4, 17, 13) as needed, but it has to go > through > junk > # [,1] [,2] [,3] [,4] > #[1,] 0 -3 -7 -20 > #[2,] 3 0 -4 -17 > #[3,] 7 4 0 -13 > #[4,] 20 17 13 0 > > Anyone have a better idea? > > -Dan > > > > -- > View this message in context: http://r.789695.n4.nabble.com/vector-manipulations-differences-tp4712575.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hi, xr <- rev( x) vec <- 1:(length( x) - 1) rev( xr[ sequence( vec)] - rep.int( xr[ -1], vec)) On 2015-09-21 14:17:40, Dan D wrote:> I need an efficient way to build a new n x (n-1)/2 vector from an n-vector x > as: > > c(x[-1]-x[1], x[-(1:2)]-x[2], ... , x[-(1:(n-1)] - x[n-1]) > > x is increasing with x[1] = 0. > > The following works but is not the greatest: > junk<-outer(x, x, '-') > junk[junk>0] > > e.g., > given > x<-c(0, 3, 7, 20) > junk<-outer(x, x, '-') > junk[junk>0] # yields: c(3, 7, 20, 4, 17, 13) as needed, but it has to go > through > junk > # [,1] [,2] [,3] [,4] > #[1,] 0 -3 -7 -20 > #[2,] 3 0 -4 -17 > #[3,] 7 4 0 -13 > #[4,] 20 17 13 0 > > Anyone have a better idea? > > -Dan > > > > -- > View this message in context: http://r.789695.n4.nabble.com/vector-manipulations-differences-tp4712575.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
And if we want to use the approach of William Dunlap for sequence.optimization , then we can write: rev( xr[ seq_len(sum(vec)) - rep.int(cumsum(c(0L, vec[-length(vec)])), vec)] - rep.int( xr[ -1], vec)) Regards. On 2015-09-22 23:43:10, Frank Schwidom wrote:> Hi, > > xr <- rev( x) > vec <- 1:(length( x) - 1) > rev( xr[ sequence( vec)] - rep.int( xr[ -1], vec)) > > > On 2015-09-21 14:17:40, Dan D wrote: > > I need an efficient way to build a new n x (n-1)/2 vector from an n-vector x > > as: > > > > c(x[-1]-x[1], x[-(1:2)]-x[2], ... , x[-(1:(n-1)] - x[n-1]) > > > > x is increasing with x[1] = 0. > > > > The following works but is not the greatest: > > junk<-outer(x, x, '-') > > junk[junk>0] > > > > e.g., > > given > > x<-c(0, 3, 7, 20) > > junk<-outer(x, x, '-') > > junk[junk>0] # yields: c(3, 7, 20, 4, 17, 13) as needed, but it has to go > > through > > junk > > # [,1] [,2] [,3] [,4] > > #[1,] 0 -3 -7 -20 > > #[2,] 3 0 -4 -17 > > #[3,] 7 4 0 -13 > > #[4,] 20 17 13 0 > > > > Anyone have a better idea? > > > > -Dan > > > > > > > > -- > > View this message in context: http://r.789695.n4.nabble.com/vector-manipulations-differences-tp4712575.html > > Sent from the R help mailing list archive at Nabble.com. > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >