I have a 3D logical array where is several spatial areas marked as "true" or "1". 1st dimension is latitude, 2nd – longitude, 3d – depth. I need to choose only one area which contains a preassigned point in space. This point is always inside one of the areas. As a result, I should get the similar logical array, which has only one area with values "1". I think the first problem is to find the border for each area. I tried to use "alphaShape" for it but I’m not sure that I actually need it. Besides, I had a problem with it because my areas stick together into one. I have added a data file in the hope that it will make my question clearer. shp = alphaShape(lon3(mask), lat3(mask), z3(mask), 1); plot(shp) % lat3, lon3, z3 - 3d array of latitudes, longitudes and depths % mask - logical 3d array with several spatial areas % core_lat, core_lon, core_z - coordinates of preassigned point (red cross % in picture) (in data) An example of data is shown in the figure below. The figure above shows a vertical section along the latitude of 69.63 N, the figure below shows the distribution at the depth of 417 meters. Yellow is several areas, from which I need to select one. Red cross is preassigned point.
Neeta Dsouza answered .
2025-11-20
RGB = imread('pillsetc.webp'); % image processing toolbox
mask = imbinarize(rgb2gray(RGB));
target = [430,170];
[rownum, colnum] = find(mask); [~,I] = pdist2([colnum,rownum],target,'cityblock','smallest',1); maskLbl = bwlabel(mask); targetGroup = maskLbl(rownum(I),colnum(I)); maskClean = mask; maskClean(maskLbl ~= targetGroup) = false;
figure()
tiledlayout(1,2)
nexttile
imagesc(mask)
colormap(parula(2))
hold on
plot(target(1), target(2), 'rx', 'DisplayName', 'target')
plot(colnum(I),rownum(I), 'ro', 'DisplayName', 'Nearest true value')
title('full binary image')
axis tight
axis equal
nexttile
imagesc(maskClean)
colormap(parula(2))
hold on
plot(target(1), target(2), 'rx', 'DisplayName', 'target')
plot(colnum(I),rownum(I), 'ro', 'DisplayName', 'Nearest true value')
title('trimmed binary image')
axis tight
axis equal
lg = legend();
lg.Layout.Tile = 'south';
