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]
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 vectorV
and vectorN
, creates a vectorW
where elementV(i)
is repeatedN(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