julia lang - Memory & Computation Efficient Creation of Array with Repeated Elements -


i trying find efficient way create new array repeating each element of old array different, specified number of times. have come works, using array comprehensions, not efficient, either in memory or in computation:

length = 1e6 = collect(1:length) ## arbitrary values repeated specified numbers of times numrepeats = [rand(20:100) idx = 1:length]  ## arbitrary numbers of times repeat each value in b = vcat([ [a[idx] n = 1:numrepeats[idx]] idx = 1:length(a) ]...) 

ideally, structure akin sparse matrix apparatus julia has instead store data efficiently based on indices repeated values occur. barring that, @ least efficient way create vector such b in example above. looked repeat() function, far can tell documentation , experimentation function, repeating slices of array same number of times each slice. best way approach this?

sounds you're looking run-length encoding. there's rlevectors.jl package here: https://github.com/phaverty/rlevectors.jl. not sure how usable is. make own data type easily.


Comments