Maybe something like this? Not pretty, but not fully brute force either. Since you are looking at fairly big chunks (1000), this should be quite fast.
For any order:
a = [8 8 8 8 8 7 7 7 7 7 6 6 6 3 3 3 1 1 1 0 0 0 0 0];
N = 5;
M = length(a);
ind = 1;
while ( ind <= M )
if ( all( a(ind) == a(ind:min(M,ind+N-1)) ) )
ind = ind + N;
while ( ind <= M && a(ind) == a(1) )
ind = ind + 1;
end
else
a(ind:min(M,ind+N-1)) = a(max(1,ind-1));
ind = ind + N-1;
end
end
Note that these weird min/max expressions are only there to make sure I don't run over the boundaries (I hope I didn't miss anything).
Now if they are sorted, I'm sure you can find tricks to avoid (almost) any loops. E.g if you want to count the occurrences and see which ones you have to replace:
a = [8 8 8 8 8 7 7 7 7 7 6 6 6 3 3 3 1 1 1 0 0 0 0 0];
M = length(a);
N = 5;
[ unique_num, index, rev_index ] = unique(a,'stable');
N_occurrence = [ diff(index) ; length(a)-index(end) + 1 ];
fix_numbers = (N_occurrence < N);
Good luck!
Best Answer