Intersection of two convex set in n-dimensional space - convex-hull

Let $X={x_1,\ldots,x_p}$ and $Y={y_1,\ldot,y_q}$ be two set of points in $\mathbb{R}^n$.
Is there a method to find $\mathrm{conv}(X)\cap \mathrm{conv}(Y)$ as $\mathrm{conv}(Z)$, where $Z$ is some subset of $\mathbb{R}^n$?

Related

Finding the region in a Voronoi diagram that contains a point

I am trying to build a Voronoi diagram using the code in this link. However, I have a few points and want to know in which region they fall. This code, like the original function in MATLAB (i.e. voronoin) gives two outputs: [vornb,vorvx], one for the vertices and another one for the cells. So, I want to see which region of the Voronoi diagram the point (x, y, z) falls in.
I am actually looking for something like this region masking in 3D.
Regardless of whether you generate your Voronoi cells using the built-in voronoin (which takes an N-by-D matrix of points X as input) or polybnd_voronoi (the linked File Exchange submission for bounded Voronoi cells, which takes an additional M-by-D matrix of points BX defining a bounding convex polyhedron), you can compute which cell contains a given point [x y z] by using only those same input arguments.
For the bounded Voronoi cell case, you first need to determine if your point is inside the bounding convex hull or not. One way to do this is to create a Delaunay triangulation of the bounding points and use the pointLocation method to determine if the point is within the convex hull:
DT = delaunayTriangulation(BX);
cellIndex = pointLocation(DT, [x y z]);
If cellIndex is NaN, then the point is not within the boundary convex hull. Otherwise, it is in one of the Voronoi cells. To determine which, first consider that a Voronoi cell C{i} represents the set of all points that are closer to point X(i, :) than any other point in X. Therefore, to find out which cell a point [x y z] falls into you just have to find the point in X that it is closest to. You can do this using pdist2 and min like so:
[~, cellIndex] = min(pdist2(X, [x y z]));
If you don't have access to pdist2 (which is in the Statistics Toolbox), you can compute the distances yourself like so:
[~, cellIndex] = min(sqrt(sum(bsxfun(#minus, X, [x y z]).^2, 2)));
Now, cellIndex can be used as an index into the output arguments from voronoin or polybnd_voronoi to get the bounding Voronoi cell.
Generalize to multiple points:
You can generalize the above to more than just one point [x y z], which will allow you to create a 3D region mask:
[PX, PY, PZ] = meshgrid(...); % Generate regular points in a 3D volume
PXYZ = [PX(:) PY(:) PZ(:)]; % Combine them into one matrix
DT = delaunayTriangulation(BX); % Create triangulation for boundary
cellMask = pointLocation(DT, PXYZ); % Find points in boundary
index = ~isnan(cellMask); % Index of points in boundary
[~, cellMask(index)] = min(pdist2(X, PXYZ(index, :)), [], 1); % Find cell index
cellMask = reshape(cellMask, size(PX)); % Reshape mask to 3D
And the 3D mask cellMask will contain index values for points within Voronoi cells and NaN for points outside the bounding convex hull.

how to find distance between consecutive points

I have a matrix
A=[51.7365160000000 10.7978860000000;
51.7366230000000 10.8319610000000;
51.7389880000000 10.7849260000000;
51.7424430000000 10.9195510000000;
51.7443820000000 10.9157750000000;
51.7448080000000 10.9160750000000;
51.7523270000000 10.8756060000000;
51.7525920000000 10.8758210000000;
51.7526190000000 10.8738470000000;
51.7526460000000 10.8763360000000;
51.7528580000000 10.8477970000000;
51.7530180000000 10.8776230000000];
The first column of A indicates latitude, the second column indicates longitude, with each row being a different point. I want to find the distance between consecutive points. I have used the function pdist in this way
a = pdist(A,'euclidean')';
but it gives the distance between all points and not only between consecutive points.
Can you help me to solve the problem?
Thanks
As pointed out in the help of pdist you can use squareform to organize the data.
b = squareform(a);
The distances between neighbouring points are then the subdiagonals of this matrix.
dist = diag(b,1)
You can also easily calculate the distances manually
dist = sqrt(diff(A(:,1)).^2+diff(A(:,2)).^2)

MATLAB: pdist, finding pixel locations of minimum pairwise distance in binary image

I have a binary image and have found the minimum distance connecting two nearby regions of interest/connected components using
min(min(pdist2(CCstats(1).PixelList,CCstats(2).PixelList)))
I also need to get the coordinates of these ends of this distance/the most adjacent pixels between these 2 regions of interest
I plan on drawing a line along this distance. I was going to use something like:
x=linspace(coord1(1), coord2(1),1000);
y=linspace(coord1(2), coord2(2),1000);
index=sub2ind(size(image),round(y),round(x));
image(index)=1;
Any suggestions for how to find these coordinates?
You will want the second output of min (combined with ind2sub) to determine the row/column of the pdist2 matrix that corresponds to the minimum distance.
distances = pdist2(CCstats(1).PixelList, CCstats(2).PixelList);
[mindist, minind] = min(distances(:));
%// Now convert this linear index into index1/index2 into your pixel lists
[index1, index2] = ind2sub(size(distances), minind);
%// Now grab the coordinates using these index values
coord1 = CCstats(1).PixelList(index1,:);
coord2 = CCstats(2).PixelList(index2,:);

Finding the diameter of a graph

In MATLAB I'm dropping points into the square [0,1]X[0,1] uniformly at random. I'm then placing an edge between two points if the distance between the two points is less than 0.25. The code to do this (and plot the resulting graph is below).
num_Rx = 50
Rx_positions = rand(num_Rx,2);
%Generate edges between pairs of within norm of 0.25.
adj_matrix = zeros(num_Rx, num_Rx);
for i=1:num_Rx
for j=i+1:num_Rx
dist = norm(Rx_positions(i,:) - Rx_positions(j,:));
if dist < 0.25;
adj_matrix(i,j) = 1;
end
end
end
figure
gplot(adj_matrix, Rx_positions)
i.e. this is an undirected graph, with all edge weights equal to 1.
To find the diameter I want to call graphallshortestpaths() (http://www.mathworks.co.uk/help/bioinfo/ref/graphallshortestpaths.html), and take the maximum value returned (the diameter is the longest shortest path).
However, I'm having difficulty calling this function - I can't figure out how to make a call like the examples, from my adjacency matrix and/or my list of node positions. Specifically I don't understand any of the examples, how the matlab code given corresponds to the data presented.
How would I go about doing this.
It appears that all you're missing is converting your adjacency matrix to a sparse matrix. This should give you what you're looking for:
adj_sparse = sparse(adj_matrix);
distances = graphallshortestpaths(adj_sparse);
% if you don't care which nodes produce the
% longest shortest path, omit long_ind & long_sub
[diameter, long_ind] = max(distances(:));
long_sub = ind2sub(size(distances), long_ind);

Finding the max/min of a discrete image

Given a discrete image, e.g.:
how can one find the local minima/maxima locations?
EDIT:
Maximum and minimum in terms of derivative, not absolute max/min. In the example below the results should be two lines at the bottom, and some local peaks at the top.
Note that deriving is not that simple since the zero locations falls between pixels, and zero crossing in a 2d image is more complected than a 1d signal.
Thanks,
Find the linear index of the max/min (second output) and then use ind2sub to get the row and column coordinates.
%Assuming your image is stored in matrix I
[Vmax, Imax] = max(I(:));
[Rmax, Cmax] = ind2sub(size(I), Imax);
[Vmin, Imin] = min(I(:));
[Rmin, Cmin] = ind2sub(size(I), Imin);