run-length-encoding

Create group names for consecutive values

元气小坏坏 提交于 2019-11-26 03:45:05
问题 Looks like an easy task, can\'t figure out a simpler way. I have an x vector below, and need to create group names for consecutive values. My attempt was using rle , better ideas? # data x <- c(1,1,1,2,2,2,3,2,2,1,1) # make groups rep(paste0(\"Group_\", 1:length(rle(x)$lengths)), rle(x)$lengths) # [1] \"Group_1\" \"Group_1\" \"Group_1\" \"Group_2\" \"Group_2\" \"Group_2\" \"Group_3\" \"Group_4\" # [9] \"Group_4\" \"Group_5\" \"Group_5\" 回答1: Using diff and cumsum : paste0("Group_", cumsum(c(1

Element-wise array replication in Matlab

让人想犯罪 __ 提交于 2019-11-26 00:29:13
问题 Let\'s say I have a one-dimensional array: a = [1, 2, 3]; Is there a built-in Matlab function that takes an array and an integer n and replicates each element of the array n times? For example calling replicate(a, 3) should return [1,1,1,2,2,2,3,3,3] . Note that this is not at all the same as repmat . I can certainly implement replicate by doing repmat on each element and concatenating the result, but I am wondering if there is a built in function that is more efficient. 回答1: As of R2015a ,

Is there a dplyr equivalent to data.table::rleid?

那年仲夏 提交于 2019-11-26 00:20:51
问题 data.table offers a nice convenience function, rleid for run-length encoding: library(data.table) DT = data.table(grp=rep(c(\"A\", \"B\", \"C\", \"A\", \"B\"), c(2, 2, 3, 1, 2)), value=1:10) rleid(DT$grp) # [1] 1 1 2 2 3 3 3 4 5 5 I can mimic this in base R with: df <- data.frame(DT) rep(seq_along(rle(df$grp)$values), times = rle(df$grp)$lengths) # [1] 1 1 2 2 3 3 3 4 5 5 Does anyone know of a dplyr equivalent (?) or is the \"best\" way to create the rleid behavior with dplyr is to do

Repeat copies of array elements: Run-length decoding in MATLAB

早过忘川 提交于 2019-11-25 23:17:15
问题 I\'m trying to insert multiple values into an array using a \'values\' array and a \'counter\' array. For example, if: a=[1,3,2,5] b=[2,2,1,3] I want the output of some function c=somefunction(a,b) to be c=[1,1,3,3,2,5,5,5] Where a(1) recurs b(1) number of times, a(2) recurs b(2) times, etc... Is there a built-in function in MATLAB that does this? I\'d like to avoid using a for loop if possible. I\'ve tried variations of \'repmat()\' and \'kron()\' to no avail. This is basically Run-length