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

How to capture an arbitrary number of groups in JavaScript Regexp?

When you repeat a capturing group, in most flavors, only the last capture is kept; any previous capture is overwritten. In some flavor, e.g. .NET, you can get all intermediate captures, but this is not the case with Javascript. That is, in Javascript, if you have a pattern with N capturing groups, you can only … Read more

Repeat a string in JavaScript a number of times

These days, the repeat string method is implemented almost everywhere. (It is not in Internet Explorer.) So unless you need to support older browsers, you can simply write: “a”.repeat(10) Before repeat, we used this hack: Array(11).join(“a”) // create string with 10 a’s: “aaaaaaaaaa” (Note that an array of length 11 gets you only 10 “a”s, … Read more