MATLAB: Which is the most appropiate loop if I want to create a vector theOrder(n) with random values

if loopwhile loop

Want to create a vector myOrder(n) from 1:Sum with random values
vec= [1 1 1 1 3 1 4 1 1 5 5]';
Sum=sum(vec);
for n=1:Sum
N=vec(n);
if vec(n)==1
myOrder(n) = rand(1); % Condition 1
elseif vec(n)>1
myOrder(n) = (n/(N+1))*(1 + myOffset*randn(1)); % Condition 2
end
end
The Problem:
I would like for every iteration in vec:
  • The loop subtracts 1 for each value > 1.
  • For every new value of vec(n)>1, Condition 2 applies until it reaches the value of 1.
I.e
vec = [1 1 1 1 3 1 4 1 1 5 5]' -> Iteration 1
vec = [1 1 1 1 2 1 3 1 1 4 4]' -> Iteration 2
vec = [1 1 1 1 1 1 2 1 1 3 3]' -> Iteration 3
vec = [1 1 1 1 1 1 1 1 1 2 2]' -> Iteration 4
vec = [1 1 1 1 1 1 1 1 1 1 1]' -> Iteration 5

Best Answer

A complete guess since the explanation of the generation is not clear (and giving us code that doesn't work isn't a replacement for a good explanation!):
vec = [1 1 1 1 3 1 4 1 1 5 5]';
vec2 = vec - (0:max(vec)-1); %requires R2016b or later
vec2 = vec2(vec2 > 0); %a vector of all the values from your iterations (with 1s only present once)
n = (1:numel(vec2))';
order = zeros(size(vec2));
condition2 = vec2 ~= 1;
order(~condition2) = rand(sum(~condition2), 1); %apply condition 1
order(condition2) = n(condition2) ./ vec2(condition2) .* (1 + myOffset * randn(sum(condition2), 1)) %apply condition 2