# 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?

조회 수: 1(최근 30일)
Wesley 2021년 1월 19일
댓글: Star Strider 2021년 1월 20일
##### 댓글 수: 1표시숨기기 없음
Wesley 2021년 1월 19일
Points C and D have been calculated.
x = data1(:, 1);
y = data1(:, 2);
%Calculate points C and D
[a,I]=min(x);
C=[a,y(I)];
[b,J]=max(x);
D=[b,y(J)];
hold on

댓글을 달려면 로그인하십시오.

### 채택된 답변

Star Strider 2021년 1월 19일
Try this:
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:
.
##### 댓글 수: 9표시숨기기 이전 댓글 수: 8
Star Strider 2021년 1월 20일
As always, my pleasure!
This was likely the most challenging anallysis I’ve done here on Answers!

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

KSSV 2021년 1월 19일
편집: KSSV 2021년 1월 19일
Option 1: You should get the indices of C, D in the given points of curve. Read about knnsearch, this will give indices of points in curve which are close/ same as C and D. Once you know these indices, you can get the upper and lower curve depending on whether the points are in clockwise direction or anti clockwise direction.
Option 2: This is simple, you know C, D; as they lie on x-axes/ parallel to x-axes, these points should have same y. So:
idx1 = P(:,2)>=C(2) ; % C(2), D(2) both will be same
dataA = P(idx1,:) ;
dataB = P(~idx1,:) ;
##### 댓글 수: 1표시숨기기 없음
Wesley 2021년 1월 19일

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by