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

后端 未结 5 1586
猫巷女王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:26

    There is finally (as of R2015a) a built-in and documented function to do this, repelem. The following syntax, where the second argument is a vector, is relevant here:

    W = repelem(V,N), with vector V and vector N, creates a vector W where element V(i) is repeated N(i) times.

    Or put another way, "Each element of N specifies the number of times to repeat the corresponding element of V."

    Example:

    >> a=[1,3,2,5]
    a =
         1     3     2     5
    >> b=[2,2,1,3]
    b =
         2     2     1     3
    >> repelem(a,b)
    ans =
         1     1     3     3     2     5     5     5
    

提交回复
热议问题