# Choosing middle points in discrete function

2 views (last 30 days)
Zine on 19 Feb 2014
Answered: Zine on 21 Feb 2014
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
Zine on 19 Feb 2014
see attached hand drawing where the black lines are my original set of data, and the blue stars are the desired new set and the red line is the link between new set points, I did not put my data because it is huge array of data, in my function it takes the last point of each step but I need the middle point,(x points in one step is variable from 1 to around 60)

Zine on 21 Feb 2014
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

### Categories

Find more on Surface and Mesh Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!