run-length-encoding
Element-wise array replication according to a count [duplicate]
Here’s one way I like to accomplish this: >> index = zeros(1,sum(rep)); >> index(cumsum([1 rep(1:end-1)])) = 1; index = 1 0 1 0 0 1 1 0 0 0 0 >> index = cumsum(index) index = 1 1 2 2 2 3 4 4 4 4 4 >> vv = v(index) vv = 3 3 … Read more
Run-length decoding in MATLAB
To find out which one is the most efficient solution, we provide a test-script that evaluates the performance. The first plot depicts runtimes for growing length of the vector runLengths, where the entries are uniformly distributed with maximum length 200. A modification of gnovice‘s solution is the fastest, with Divakar‘s solution being second place. This … Read more
Create group names for consecutive values
Using rleid from data.table, library(data.table) rleid(x, prefix = “Group_”) #[1] “Group_1” “Group_1” “Group_1” “Group_2” “Group_2” “Group_2” “Group_3” “Group_4” “Group_4” “Group_5” “Group_5”
Element-wise array replication in Matlab
I’m a fan of the KRON function: >> a = 1:3; >> N = 3; >> b = kron(a,ones(1,N)) b = 1 1 1 2 2 2 3 3 3 You can also look at this related question (which dealt with replicating elements of 2-D matrices) to see some of the other solutions involving matrix … Read more
Create counter within consecutive runs of values
You need to use sequence and rle: > sequence(rle(as.character(dataset$input))$lengths) [1] 1 1 2 1 2 1 1 2 3 4 1 1
Repeat copies of array elements: Run-length decoding in MATLAB
Problem Statement We have an array of values, vals and runlengths, runlens: vals = [1,3,2,5] runlens = [2,2,1,3] We are needed to repeat each element in vals times each corresponding element in runlens. Thus, the final output would be: output = [1,1,3,3,2,5,5,5] Prospective Approach One of the fastest tools with MATLAB is cumsum and is … Read more
Is there a dplyr equivalent to data.table::rleid?
You can just do (when you have both data.table and dplyr loaded): DT <- DT %>% mutate(rlid = rleid(grp)) this gives: > DT grp value rlid 1: A 1 1 2: A 2 1 3: B 3 2 4: B 4 2 5: C 5 3 6: C 6 3 7: C 7 3 8: … Read more