MATLAB: Replace repeatative values by using interpolation

curveMATLABrepeatative valuesuniqueunique values

Hi Matlabers ,
I have a vector which has repeatative values from 0 to 1.
I want to relace those repeataive values by using interpolated values in matlab script. As a result, I will have no any repeatative value and the plot/ and curve of that vector will be smooth.
Thanks in advance.

Best Answer

Try this
x = [0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1]'; % generate random data
indexes = [1; find(x(2:end) - x(1:end-1))+1];
x_new = zeros(size(x));
for i=1:numel(indexes)-2
x_new(indexes(i):indexes(i+1)) = interp1([0 1], x(indexes([i i+1])), linspace(0, 1, indexes(i+1)-indexes(i)+1));
end
x_new(indexes(end-1):end) = interp1([0 1], x([indexes(end-1) end]), linspace(0, 1, numel(x)-indexes(end-1)+1));