Filling in polygon in matrix - matlab

In Matlab, I have a boolean matrix of zeros, and a list of points.
E.g.:
matrix = zeros(x, y);
points = [x1 y1; x2 y2; x3 y3; x4 y4];
How do I set all the bits in the matrix enclosed by the polygon formed by these points?
I'm aware of the fill function but that seems to just plot the shape rather than filling in the matrix.

I would suggest using inpolygon or poly2mask matlab functions.

Related

How to make a 2D contour plot with given data point in Octave/MATLAB?

I have a matrix whose three columns correspond to x, y and f values. I want to make a contour plot of f(x,y) in the x,y plane from these data with Octave/MATLAB.
Let's say, the matrix M is
x1 y1 f1
x2 y2 f2
x3 y3 f3
. . .
. . .
I found the function contourf requires f to be a matrix (whereas I have a vector with corresponding points).
How to generate this plot?
The x, y, and z variables that you pass to contourf are all matrices of the same size. For every point you need an x, y, and z value. You can use meshgrid to make matrices that have all the combinations of x and y values.
This example is from the doc for contourf. I added some comments to explain what is happening
% Create a vector of x values
x = linspace(-2*pi,2*pi);
% Create a vector of y values
y = linspace(0,4*pi);
% Make matrices with all combinations of x and y values for plotting
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
contourf(X,Y,Z)
This is the result of the above code

Using Surf and Peaks for non square Matrices

Im trying to use meshgrid with peak. When I use square matrices like:
[x,y] = meshgrid(1:10,1:10)
z = peaks(10)
surf(x,y,z)
Everything works fine.
However, when I use Non square matrices:
[x,y] = meshgrid(-30000:500:0,10:500);
z = peaks(?)
surf(x,y,z)
Im getting the following error:
Data dimensions must agree.
Any idea how to make it work?
Thank you.
When you plot a function using surf the variables you pass it must all be the same size. Although you are changing the x and y matrices to non-square matrices, you are not creating a correspondingly sized z matrix. If you look at the documentation for peaks, this tells you that this function can only produce square outputs, which complicates things somewhat. I would suggest that you try to plot some other example function instead.
If you absolutely must plot the peaks function, you could use some kind of interpolation:
x = 1:10;
y = 1:10;
[x_mesh, y_mesh] = meshgrid(x,y);
z = peaks(10);
x2 = 1:0.1:10;
y2 = 1:0.5:10;
[x2_mesh, y2_mesh] = meshgrid(x2, y2);
z2 = interp2(x_mesh, y_mesh, z, x2_mesh, y2_mesh);
surf(x2_mesh, y2_mesh, z2);
This gives me the following plot:

Gaussian Contour plot of 3 variables - MATLAB

I have generated a 3D plot that resembles Gaussian distribution, with random variables Y, X1, and X2 (1000x1) vectors. Y is on the vertical axis, X1, and X2 are horizontal.
Specifically, this is the code I used for the plot:
plot3(x(:,1),x(:,2),y,'.')
The graph that has been created has this form:
What I also want to produce is something like that:
But, when I use this code:
contour(x(:,1),x(:,2),y);
I receive a message that:
Error using contour (line 48)
Z must be at least a 2x2 matrix.
I really don't get how to fix that problem, I assume Z is the Y but I don't understand why it has to be 2x2 at least. Anyhow, any help would be much appreciated.
You cannot create a contour over scattered data, you need a grid. It is possible to interpolate the data on a grid of NxN samples in the XY domain, using griddata (here the domain is [-2,2]x[-2,2] as an example):
N = 200;
xi = linspace(-2, 2, N);
yi = linspace(-2, 2, N);
[XI, YI] = meshgrid(xi, yi);
ZI = griddata(x(:,1), x(:,2), y, XI, YI, 'v4');
contour(XI,YI,ZI);
More info on how to interpolate scattered data here.

Finding the equation of a contour in Matlab

I have a contour plot generated by Matlab. I wish to know the equation of one of the contours drawn by contour function.
Is there a simple way to do this?
Thanks.
P.S. I am new here, so the site didn't let me attach an image file saying I need enough reputation to post images!
From doc contour:
[C,H] = CONTOUR(...) returns contour matrix C as described in CONTOURC
and a handle H to a contourgroup object. This handle can be used as
input to CLABEL.
And from contourc:
The contour matrix C is a two row matrix of contour lines. Each
contiguous drawing segment contains the value of the contour,
the number of (x,y) drawing pairs, and the pairs themselves.
The segments are appended end-to-end as
C = [level1 x1 x2 x3 ... level2 x2 x2 x3 ...;
pairs1 y1 y2 y3 ... pairs2 y2 y2 y3 ...]

Matlab:Make a contour plot with 3 vectors

I have 3 vectors of data, X (position), Y (position) both of which are not regularly spaced, and Z(value of interest at each location). I tried contourf, which doesn't work because it needs a matrix for Z input.
You can also use griddata.
%Generate random data
x = rand(30,1);
y = rand(30,1);
z = rand(30,1);
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x),max(x),n), linspace(min(y),max(y),n))
%create contour plot
contour(X,Y,griddata(x,y,z,X,Y))
%mark original data points
hold on;scatter(x,y,'o');hold off
For a contour plot you actually need either a matrix of z values, or a set (vector) of z-values evaluated on a grid. You cannot define contours using isolated Z values at (X,Y) points on the grid (i.e. what you claim you have).
You need to have the generating process (or function) provide values for a grid of (x,y) points.
If not, then you can create a surface from nonuniform data as #nate correctly pointed out, and then draw the contours on that surface.
Consider the following (random) example:
N = 64; % point set
x = -2 + 4*rand(N,1); % random x vector in[-2,2]
y = -2 + 4*rand(N,1); % random y vector in[-2,2]
% analytic function, or z-vector
z = x.*exp(-x.^2-y.^2);
% construct the interpolant function
F = TriScatteredInterp(x,y,z);
t = -2:.25:2; % sample uniformly the surface for matrices (qx, qy, qz)
[qx, qy] = meshgrid(t, t);
qz = F(qx, qy);
contour(qx, qy, qz); hold on;
plot(x,y,'bo'); hold off
The circles correspond to the original vector points with values (x,y,z) per point, the contours on the contours of the interpolant surface.