Use the index of an element of a subset to find its index in the large set?

9 views (last 30 days)
function d_z = distance(tip, samp, search_dis)
%tip is a n by 3 matrix, where each row is just the coordinate a point in 3-d space. It is a collection of all points in the tip.
%samp is a n by 3 matrix, where each row is just the coordinate of a point in 3-d space. It is a collection of all points in the sample.
%search_dis is a scalar.
%I divide the whole 3-d space into columns. [X, Y] is just the positions of each column in 3d space.
X = min(samp(:, 1)):search_dis:max(samp(:, 1));
Y = min(samp(:, 2)):search_dis:max(samp(:, 2));
num_cols = length(X);
num_rows = length(Y);
%Create a array to store the smallest vertical distance between points from tips and sample in the same column for each column.
d_z = zeros(num_rows, num_cols)
for i = 1:num_rows
for j = 1: num_cols
%I find the index of all the points within i,jth column.
x_r = [X(i)-search_dis/2, X(i)+search_dis/2];
y_r = [Y(j)-search_dis/2, Y(j)+search_dis/2];
samp_ind = samp(:, 1)>=x_r(1)&samp(:, 1)<=x_r(2)&samp(:, 2)>=y_r(1)&samp(:, 2)<=y_r(2);
tip_ind = tip(:, 1)>=x_r(1)&tip(:, 1)<=x_r(2)&tip(:, 2)>=y_r(1)&tip(:, 2)<=y_r(2);
%if there's no point from the sample in the column, skip. If there's no point from the tip in the column, skip.
if isempty(tip)|isempty(samp)
continue
else
%if there're points from both the sample and the tip in the column, find the index of the lowest point
%from the tip (point have the smallest z value).
[tip_z, I_tip] = min(tip(tip_ind, 3));
%If there're points from both the sample and the tip in the column, find the index of the highest point
%from the tip (point have the biggest z value).
[samp_z, I_samp] = max(samp(samp_ind, 3));
%Calculate the vertical distance between these two points (the difference in z component) and store them in
%the i,jth position of d_z (the shortest vertical distance in the i,jth column).
d_z(i, j) = tip_z-samp_z;
end
end
end
%find the index of the smallest nonzero value in the d_z
[d, I] = min(nonzeros(d_z))
%==============================================================================================================================
%How do I use all these indexs to find the index of the two points (in terms of their index in tip and samp)that from sample and
% tip respectively that that satisfy the condition above. Namely the vertical distance between such two element is d.
%============================Solution?========================================================================================
%==============================================================================================================================
end
Could anyone find a way to find the index of such two points in the origional samp and in the origional tip?
Example:
The image of tip and sample is displayed in "samp_image.pdf". The purpose of the algorithm just to find the shortest vertical distance between the tip and the sample. Then return the index of the two points in the tip and sample that construct this shortest vertical distance.
Example of the input:
tip =[2, 3, 2; 1, 0, 2; 3, 2, 4];
samp = [2, 3, 1; 2, 3, 0.2; 0.2, 2, 3];
In tip and sample, each row just corresponds to the coordinate of a points in 3d space.
Then index of a point is just the index of the row, because each row corresponds to a point.
If I found the shortest vertical distance is between [2, 3, 2] and [2, 3, 1];
then I would like the algorithm to return the index of the point in the tip and the index of the point in the sample as [1,1].

Accepted Answer

dpb
dpb on 22 Jun 2019
tipPt=[2 3 2]; % the two located points
smpPt=[2 3 1];
% return their locations in the two arrays, respectively
>> [find(ismember(tip,tipPt,'rows')) find(ismember(samp,smpPt,'rows'))]
ans =
1 1
>>
Presuming you only pass actual datasets that match an element in each array by the previous calculation, the above should be robust to not require additional error checking.

More Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!