Evenly distribute range of specified values within a vector

matlab, range, vector

Solution

Say you want to distribute the values `[5 2 6]` as evenly as possible on a vector of length 8. Then you can use `linspace` the following way:

vals = [5 2 6];
vecLength = 8;

nVals = length(vals);
idx = floor(linspace(1,nVals+1-2*eps(nVals),vecLength));

out = vals(idx)

out =

     5     5     5     2     2     6     6     6

Problem

I have a vector A and I want to populate it with values as evenly as possible. For example, if A is 1x30 and I want to use three values I would use a code like this below: ``` % A = zeros(1,30); A([1:10])=0; A([11:20])=1; A([21:30])=2; ``` This works, but seems a bit cumbersome to me. Is there a more elegant way to evenly (as possible) distribute a specified range of values within a vector? I am intent on keeping each of the values in "clumps." Thank you kindly in advance.

Original source