MATLAB: Hello everyone, how to divide a piece of data into upper and lower parts along the y-axis with C point and D point as the boundary

divide a piece of data into upper and lower parts

Best Answer

Try this:
D = load('data1.mat');
data1 = D.data1;
x = data1(:, 1);
y = data1(:, 2);
[C,ixmin] = min(x);
[D,ixmax] = max(x);
B = [x(ixmin) 1; x(ixmax) 1] \ [y(ixmin); y(ixmax)];
[xsort,sortidx] = sort(x);
fitline = [xsort ones(size(xsort))] * B;
topidx = y(sortidx) >= fitline;
figure
plot(x(sortidx(topidx)), y(sortidx(topidx)), '.r')
hold on
plot(x(sortidx(~topidx)), y(sortidx(~topidx)), '.g')
plot(xsort, fitline, '-b')
plot(C,y(ixmin), 'bo', 'MarkerFaceColor','b')
plot(D,y(ixmax), 'bo', 'MarkerFaceColor','b')
hold off
xlabel('X')
ylabel('Y')
legend('Above Line', 'Below Line', 'Line', 'Location','SE')
text(C,y(ixmin), 'C ', 'HorizontalAlignment','right', 'VerticalAlignment','middle')
text(D,y(ixmax), ' D', 'HorizontalAlignment','left', 'VerticalAlignment','middle')
producing:
.