Generate a Sequence of Numbers with Repeated Intervals

Generate a sequence of numbers with repeated intervals

I find the sequence function to be helpful in this case. If you had your data in a structure like this:

(info <- data.frame(start=c(1, 144, 288), len=c(6, 6, 6)))
# start len
# 1 1 6
# 2 144 6
# 3 288 6

then you could do this in one line with:

sequence(info$len) + rep(info$start-1, info$len)
# [1] 1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293

Note that this solution works even if the sequences you're combining are different lengths.

Generate a irregular sequence of numbers based on a vector of intervals in R

c(start + cumsum(c(0, intervals)), end)
# [1] -100 -90 -85 15 19 100

Then "end" is just tacked on the end. You can do something else with it, but you'll have to decide what would happen in the case your intervals go over it.

Also note that your stated requirement,

I want the length of my sequence to be same length as the interval vector.

is contradicted by your desired output. If you want different behavior, you'll need to be more precise.

Create sequence of repeated values, in sequence?

You missed the each= argument to rep():

R> n <- 3
R> rep(1:5, each=n)
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
R>

so your example can be done with a simple

R> rep(1:8, each=20)

Repeating a repeated sequence

You can do it with a single rep call. The each and times parameters are evaluated sequentially with the each being done first.

rep(1:4, times=3, each=3)  # 'each' done first regardless of order of named parameters
#[1] 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4

Sequence with repeating numbers

Another option

library(dplyr)
df %>%
mutate(pair = as.integer(gl(n(), 2, n())))
# id color pair
#1 1 red 1
#2 2 red 1
#3 3 red 2
#4 4 red 2
#5 5 red 3
#6 6 blue 3
#7 7 blue 4
#8 8 blue 4
#9 9 blue 5
#10 10 blue 5

Or with rep and cumsum

df %>% 
mutate(pair = cumsum(rep(c(TRUE, FALSE), length.out = n())))

Or much simpler case with base R

df$pair <- c(TRUE, FALSE)
df$pair <- cumsum(df$pair)

Generate sequence of repeating, ascending integers, using a list containing the number of repetitions for each, with thrust

You can do this purely with thrust, using an approach similar to yours.

  1. Do a prefix sum on the input to determine size of result for step 2, and scatter indices for step 3
  2. Create an output vector to hold the result
  3. scatter ones to the appropriate locations in the output vector, given by the indices from step 1
  4. do a prefix sum on the output vector.

Note that this method would have to be modified if the input reps vector is allowed to contain values of 0.

Here's a worked example:

$ cat t404.cu
#include <thrust/scan.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <iostream>

int main(){

int host_reps[] = {3, 2, 5, 1};
int ds = sizeof(host_reps)/sizeof(int);
thrust::device_vector<int> reps(host_reps, host_reps+ds);
thrust::inclusive_scan(reps.begin(), reps.end(), reps.begin());
thrust::device_vector<int> result(reps[reps.size()-1]);
thrust::copy_n(thrust::constant_iterator<int>(1), reps.size()-1, thrust::make_permutation_iterator(result.begin(), reps.begin()));
thrust::inclusive_scan(result.begin(), result.end(), result.begin());
thrust::copy_n(result.begin(), result.size(), std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
}
$ nvcc -o t404 t404.cu
$ ./t404
0,0,0,1,1,2,2,2,2,2,3,
$


Related Topics



Leave a reply



Submit