Suppose I have an array of length `N`. I want to choose `n` positions randomly, make them zero and then add the existing elements to the next non-zero element. 

For example, suppose `r = (r1,r2,r3,r4,r5)`, `N = 5`. Let `n = 2`. And the randomly picked positions are 3rd and 4th. Then I want to transform `r` to 
`r_new = (r1, r2, 0, 0, r3+r4+r5)`.

Instead if the randomly selected positions were `1` and `3`, then I want to have
`r_new = (0, r1 + r2, 0, r3+r4, r5)`. 

I am coding in MATLAB. Here is my current code. 

```
u   = randperm(T);
ind = sort(u(1:n(i)));
tmp = r(ind);
r(ind) = 0;

x = find( r );

```

I am not necessarily looking for MATLAB code. Pseudocode would be helpful enough.