Draw boundary around grid of data of two classes

2 views (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 4 Apr 2015
You can restrict contour to draw only one contour line. See Display Single Contour Line in the contour documentation.
  2 Comments
Mike Gates
Mike Gates on 4 Apr 2015
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 on 4 Apr 2015
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Contour 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!