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!