"Matt J" wrote in message <jdkmfn$s3b$
1...@newscl01ah.mathworks.com>...
> xnew=strrep([x,0],[ones(1,K),0],[ones(1,K-1),0]);
> xnew(end)=[];
- - - - - - - -
Andrea, the loop I referred to might look something like this:
y = [x,0];
c = 0;
for p = 1:length(y)
if y(p) == 1
c = c + 1;
else
if c > k
y(p+(-mod(c,k):-1)) = 0;
end
c = 0;
end
end
y(end) = [];
You ought to compare its timing with any vectorized solution. It might surprise you.
By the way, the vectorized solution I gave you could be simplified to:
t = diff([0,x,0]);
f1 = find(t==1);
f2 = find(t==-1)-f1;
f2 = f2-(f2>k).*mod(f2,k)+f1;
y = zeros(1,size(x,2)+1);
y(f1) = 1;
y(f2) = -1;
y(end) = [];
y = cumsum(y);
Matt, as it stands I think your suggestion would reduce the length of the vector. Perhaps you meant this
xnew=strrep([x,0],[ones(1,K),0],[ones(1,K-1),0,0]);
instead. However, as I understand Andrea's request, with say k = 10 a sequence of 19 consecutive ones should be replaced by 10 ones followed by 9 zeros, so even the above modification would not accomplish this. It would need to be performed k-1 times to be certain of success in all cases. (At least that's true with the way 'strrep' works on my antiquated system.)
Roger Stafford