MATLAB: Choosing middle points in discrete function

datadata manipulationmath functionMATLABmedium valuesplot

I have a set of experimental data y=f(x), this set looks like a discrete function means for y=a there are several corresponding x, the overall variation is sin(x) like shape like steps going up and down along with x axis. My question is, can any one help me to remove the repeated y from each step and create another set newy=f(newx) that passes through the middle point of each step, so then I will plot both as plot(x,y,'x',newx,newy) and I have the plot(newx,newy) passing exactly in the middle point of each step; thanks in advance and sorry for the long explanation

Best Answer

The problem is solved by introducing a counter in the algorithm that calculates the number of repeated data and than divide that number by two as follows
function [ nx,ny ] = nrp( xdata,ydata)
%UNTITLED Summary of this function goes here % Detailed explanation goes here
imax=length(xdata);
count=1;
j=1;
for i=1:imax-1
if ydata(i)==ydata(i+1)
count=count+1;
else
midcount=int16(count/2);
nx(j)=xdata(i-midcount+1);
ny(j)=ydata(i-midcount+1);
count=1;
j=j+1;
end
end
nx=[nx xdata(length(xdata))];
ny=[ny ydata(length(ydata))];
end