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

后端 未结 5 1491
猫巷女王i
猫巷女王i 2020-11-22 08:36

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]
5条回答
  •  情话喂你
    2020-11-22 09:28

    There's no built-in function I know of, but here's one solution:

    index = zeros(1,sum(b));
    index([1 cumsum(b(1:end-1))+1]) = 1;
    c = a(cumsum(index));
    

    Explanation:

    A vector of zeroes is first created of the same length as the output array (i.e. the sum of all the replications in b). Ones are then placed in the first element and each subsequent element representing where the start of a new sequence of values will be in the output. The cumulative sum of the vector index can then be used to index into a, replicating each value the desired number of times.

    For the sake of clarity, this is what the various vectors look like for the values of a and b given in the question:

            index = [1 0 1 0 1 1 0 0]
    cumsum(index) = [1 1 2 2 3 4 4 4]
                c = [1 1 3 3 2 5 5 5]
    

    EDIT: For the sake of completeness, there is another alternative using ARRAYFUN, but this seems to take anywhere from 20-100 times longer to run than the above solution with vectors up to 10,000 elements long:

    c = arrayfun(@(x,y) x.*ones(1,y),a,b,'UniformOutput',false);
    c = [c{:}];
    

提交回复
热议问题