title: Generate a vector of repeating values sequences, with a given number of occurrences of each value
body: <p>I would like to implement the following function:</p>
  
  <p><strong>Input:</strong></p>
  
  <ol>
  <li><p>Vector of values</p></li>
  <li><p>Vector of the same size, stating the number of occurrences each value (of the corresponding index) should have in the output vector.</p></li>
  </ol>
  
  <p><strong>Output:</strong></p>
  
  <p>A vector of the values, 1by1 in repeating sequences, where each value appears as it's needed number of occurrences.</p>
  
  <p>The values will continue appearing 1by1, untill a value appeared as many times as needed, then the rest of the values will continue appearing without it.</p>
  
  <p><strong>Example:</strong></p>
  
  <p>Inputs:</p>
  
  <ol>
  <li><p>[1,2,3,4]</p></li>
  <li><p>[3,2,5,1]</p></li>
  </ol>
  
  <p>Output:
  [1     2     3     4     1     2     3     1     3     3     3]</p>
  
  <p><strong>Wanted Solution:</strong></p>
  
  <p>I would like to find a solution that is simple, but does not use any loops, and is modular to any length of the input vectors.</p>
  
  <p><strong>Current Solution:</strong></p>
  
  <p>So far only managed to implement with loops or with unpleasant indexing. The solution with loops is as follows:</p>
  
  <p>Double Loop:</p>
  
  <pre><code>vals_vec=1:4;
  occur_vec=[3,2,5,1];
  output_vec=zeros(1,sum(occur_vec));
  num_of_vals=length(vals_vec);
  output_i=1;
  
  while (output_i&lt;=length(output_vec)) % While in length of output vector
      for cur_val_i=1:num_of_vals % Loop over all values
          if(occur_vec(cur_val_i)&gt;0) % If value hasn't reached its occurrence number
              occur_vec(cur_val_i)=occur_vec(cur_val_i)-1;
              output_vec(output_i)=vals_vec(cur_val_i);
              output_i=output_i+1;
          end
  
      end
  end
  output_vec
  </code></pre>
  
  <p>Single Loop:</p>
  
  <pre><code>vals_vec=1:4;
  occur_vec=[3,2,5,1];
  output_vec=[];
  
  for cur_num_of_vals=length(vals_vec):-1:1
      [min_val,min_i]=min(occur_vec); % Find lowest occurrence number
      output_vec=[output_vec,repmat(vals_vec,1,min_val)]; % Add vals accordingly
      vals_vec=[vals_vec(1:min_i-1),vals_vec(min_i+1:cur_num_of_vals)]; % Remove the corresponding val
      occur_vec=occur_vec-min_val; % Reduce Occurences from all vals
      occur_vec=[occur_vec(1:min_i-1),occur_vec(min_i+1:cur_num_of_vals)]; % Remove the corresponding occurrence number
  end
  
  output_vec
  </code></pre>
  
  <p>Thanks!</p>