Draw boundary around grid of data of two classes
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a grid of positive (red +) and negative (blue o) data plotted as seen below:
I'm looking to draw a boundary around the positive and negative data such as to get something like:
I've been hinted at the contour and contourf function, but am not sure how to make those work to draw such a boundary. contourf seems to give the fill I am looking for as well, but a simple boundary would suffice if the fill is otherwise much more difficult to obtain.
The grid of points X is a 1600x2 matrix (creating the 40x40 grid you see above) and the corresponding positive/negative class labels are y, which is a 1600x1 vector. y(k) == 1 represents positive class, y(k) == 0 represents negative class.
The current code to plot the data is:
% Create New Figure
figure; hold on;
% Find Indices of Positive and Negative Examples
pos = find(y==1); neg = find(y==0);
% Plot Examples
plot(X(pos, 1), X(pos, 2), 'r+');
plot(X(neg, 1), X(neg, 2), 'bo');
hold off;
If it helps, X was created using a meshgrid call.
댓글 수: 0
채택된 답변
Star Strider
2015년 4월 4일
You can restrict contour to draw only one contour line. See Display Single Contour Line in the contour documentation.
댓글 수: 2
Star Strider
2015년 4월 4일
My pleasure!
Also, if you pull out the single contour ‘x’ and ‘y’ values from the handle for your contour call, the first column has the level in row #1 and the number of (x,y) pairs in row #2. In multiple contours at the same level, this repeats for each one of them, but in the same (2xN) matrix at each level (you have one level). Don’t include the first column or subsequent ‘initial columns’ in your plot or other data, or you could get very strange discontinuities if you go to plot them outside of the contour function.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!