Find boundary of meshgrid

21 vues (au cours des 30 derniers jours)
Paul Robbins
Paul Robbins le 4 Fév 2019
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);

Réponses (2)

Pieter Livens
Pieter Livens le 9 Fév 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

Image Analyst
Image Analyst le 9 Fév 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

Community Treasure Hunt

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

Start Hunting!

Translated by