Replace repeatative values by using interpolation ?

4 次查看(过去 30 天)
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.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-3-14
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));
  2 个评论
Koustubh Shirke
Koustubh Shirke 2020-3-14
Hi Ameer,
Thanks for the answe. Its really helpfu.
It seems , it keeps first value of start of any duplicate. Can I also shift it to centre ? So that I will keep all centred value of duplictaes and then interploate remained ?
Ameer Hamza
Ameer Hamza 2020-3-14
try something like this
x = [0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0]'; % generate random data
indexes = find(x(2:end) - x(1:end-1))+1;
indexes = floor(movmean(indexes, 2, 'Endpoints', 'discard'));
x_new = zeros(size(x));
x_new(1:indexes(1)) = interp1([0 1], x([1 indexes(1)]), linspace(0, 1, indexes(1)));
for i=1:numel(indexes)-1
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):end) = interp1([0 1], x([indexes(end) end]), linspace(0, 1, numel(x)-indexes(end)+1));

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by