Draw boundary around grid of data of two classes

조회 수: 1 (최근 30일)
Mike Gates
Mike Gates 2015년 4월 4일
댓글: Star Strider 2015년 4월 4일
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.

채택된 답변

Star Strider
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
Mike Gates
Mike Gates 2015년 4월 4일
Thank you, that example helped. I additionally found out for myself where I was being confused with the X and Y matrix parameters to contour. They are not what I called X and y in my code. The X and Y coordinates of my X matrix were the ones to be passed to contour, and z was my y matrix.
Star Strider
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 CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by