Looking for a way to partition an alphaShape object between its respective alphaShape sub-regions.
Mostrar comentarios más antiguos
I have an alphaShape object (shp) made of 11 distinct sub-regions (N=11).
I want to partition my alphaShape between it's 11 respective sub-regions.
I want to plot each region in a different color and compute its respective area.
x and y are the coordinates of shp.
For n=1, I compute the x-y coordinates of the sub-region (x1 & y1) , make a new alphaShape (shp1) and compute its area (area1).
I redo these steps for n=2,3,...,N.
The file data.mat attached contains the variables shp, x and y.
Here what it looks like for n=1:
Load('data.mat')
N = numRegions(shp); %# of regions in shp
%For n=1
tf1 = inShape(shp,x,y,1); %true (1) for points in region n=1
index1 = find(tf1==1); %corresponding index for region n=1
x1 = x(index1); %corresponding x-coord for region n=1
y1 = y(index1); %crresponding y-coord for region n=1
shp1 = alphaShape(x1,y1,1); %create new alphaShape object for region n=1 with alpha radius 1
area1 = area(shp1); %area for alphaShape object for region n=1
%Redo the steps for n=2,3,...,N
It works but I feel like I am doing a lot of unnecassary steps.
I am looking for a clever way to use the sub-regions of an alphShape object as distinct alphaShape objects on their own.
Thanks
Respuestas (1)
Hi A LL
To streamline the process of partitioning your alphaShape object into its respective sub-regions, plotting each in a different color, and computing their areas, you can utilize MATLAB's capabilities more efficiently. The approach you've outlined does repeat several steps that can be optimized. Here's a more efficient way to achieve your goal:
- Use "inShape" more efficiently: "inShape" is used to determine which points belong to which region. However, please avoid calling "find" by directly using logical indexing with "tf1" to extract x and y coordinates.
- Loop through all regions: Instead of manually repeating the steps for each region, use a loop to iterate over all regions.
- Plotting: To plot each region in a different color, use MATLAB's built-in color maps or specify a list of colors.
Please refer to the below code snippet for a more efficient code:
load('data.mat');
N = numRegions(shp); % Number of regions in shp
areas = zeros(N, 1); % Pre-allocate array to store areas
figure;
hold on;
% Colors for plotting each region
colors = lines(N); % This generates a colormap with N distinct colors
for n = 1:N
tf = inShape(shp, x, y, n); % Logical array for points in region n
% Extracting x and y coordinates for region n using logical indexing
xn = x(tf);
yn = y(tf);
shpn = alphaShape(xn, yn, 1); % Create new alphaShape object for region n with alpha radius 1
areas(n) = area(shpn); % Compute and store area for region n
% Plot region n in a different color
plot(shpn, 'FaceColor', colors(n,:), 'FaceAlpha', 0.5, 'EdgeColor', 'none');
end
hold off;
% Optionally, print areas
disp('Areas of each sub-region:');
disp(areas);
Please refer to the following documentation links to learn more about "inShape", "alphaShape" and "lines" fucntions.
- https://www.mathworks.com/help/matlab/ref/alphashape.inshape.html
- https://www.mathworks.com/help/matlab/ref/alphashape.html
- https://www.mathworks.com/help/matlab/ref/lines.html
Hope this helps!
Categorías
Más información sobre Bounding Regions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
