![boundary.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203357/boundary.png)
Find boundary of meshgrid
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Trying to return a dataset of points that are the boundary of a 2d meshgrid. I am trying to use boundary() but without success so far. Does anyone know if this is possible with this command?
x = linspace(0,1,10);
[X,Y] = meshgrid (x);
ctrs(:,1) = X(:); ctrs(:,2)= Y(:);
plot(ctrs(:,1),ctrs(:,2),'.');
k=boundary(X,Y);
0 comentarios
Respuestas (2)
Pieter Livens
el 9 de Feb. de 2019
I think the trick is to reshape the data to a vector containing all values and finding the boundary in this list.
For example:
% Create data
x = linspace(-1, 1);
y = linspace(-1, 1);
[X, Y] = meshgrid(x, y);
Z = exp(-X.^2-Y.^2);
% Reshape meshgrid to N X 1 array
[nRow, nCol] = size(X);
xList = reshape(X, [nRow*nCol, 1]);
yList = reshape(Y, [nRow*nCol, 1]);
zList = reshape(Z, [nRow*nCol, 1]);
% Get indices of boundary
k = boundary(xList, yList, 1);
You can now use the indices "k" to plot the 2D curve:
% Visualize the boundary in red using RGB colors
figure()
surf(X, Y, Z, 'linestyle','none')
hold on
plot3(xList(k), yList(k), zList(k), 'Linewidth', 5, 'Color', [255, 0, 0] / 255)
![boundary.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203357/boundary.png)
0 comentarios
Image Analyst
el 9 de Feb. de 2019
This will do it. Your code plots the dots (grid point pattern), and the code I added at the end gets the outer boundary from X and Y and plots the red line (which covers up the outer blue dots).
% Define dot array.
x = linspace(0,1,10);
[X,Y] = meshgrid (x);
gridPoints(:,1) = X(:); gridPoints(:,2)= Y(:);
% Plot dots.
plot(gridPoints(:,1),gridPoints(:,2),'.');
% Define boundary
topEdge = [X(1,:)', Y(1,:)']
rightEdge = [X(:, end), Y(:, end)]
bottomEdge = flipud([X(end,:)', Y(end,:)'])
leftEdge = flipud([X(:, 1), Y(:, 1)])
completeBoundary = [topEdge; rightEdge; bottomEdge; leftEdge];
% Plot boundary.
hold on;
plot(completeBoundary(:, 1), completeBoundary(:, 2), 'r-', 'LineWidth', 3);
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203382/0000%20Screenshot.png)
0 comentarios
Ver también
Categorías
Más información sobre Surface and Mesh Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!