Find boundary of meshgrid
21 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Pieter Livens
2019-2-9
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)
0 个评论
Image Analyst
2019-2-9
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!