How to locate the coordinates of a calculated distance below a certain threshold?

5 visualizaciones (últimos 30 días)
I'll try to be as detailed as possible here:
I have two matrices with coordinates. Once matrix has coordinates of line intercepts of a synthetic microstructure, and one matrix has the coordinates of triple points of grain boundaries. The matrices are never guaranteed to be the same size. For example, the matrices would look similar to:
tpoint = [1 2
4 5
7 8
10 11];
xyints = [12 16
2 18
24 19];
Side note: in some instances, 'xyints' could be larger than 'tpoint', since I am randomly generating lines across the microstructure and the number of intercepts will vary with each run of the script, but the number of triple points is constant. The matrices listed above are just general examples; the real ones have 100+ ordered pairs within each matrix.
Essentially what I need to do is start with the first coordinate of the 'tpoint' matrix and calculate the distance between that point and each of the coordinates of the 'xyints' matrix. Here is the code I currently have to loop between 'tpoint' and 'xyints':
abc = size(tpoint);
for m = 1:abc(1)
dist = sqrt((tpoint(m,1)-xyints(1:end,1)).^2+(tpoint(m,2)-xyints(1:end,2)).^2);
end
From here, I use logical statements to find out when 'dist' is under a certain threshold, say for instance dist < 12. What I'm trying to do is locate which 'xyints' coordinates were used in the equation to produce that distance and then plot them using scatter. If any other details are needed I am happy to provide them. I am currently using the prerelease of R2020b to run this code. Any assistance you can give me is greatly appreciated.

Respuesta aceptada

Matt J
Matt J el 13 de Jul. de 2020
Wouldn't it just be
subset = xyints(dist<12,:)
  3 comentarios
Image Analyst
Image Analyst el 13 de Jul. de 2020
Editada: Image Analyst el 13 de Jul. de 2020
Be aware that in your loop dist is getting overwritten every time so when the loop exits, only the distances of xyints from the last row of tpoint are in there.
abc = size(tpoint);
for m = 1:abc(1)
dist = sqrt((tpoint(m,1)-xyints(1:end,1)).^2+(tpoint(m,2)-xyints(1:end,2)).^2); % Replaces dist every time.
end
Are you assigning subset in every iteration, or after the loop? It would need to be in the loop, but then it will change every time so it should be a cell array to keep it from getting overwritten. Look below and see that my code finds all four point pairs, not just two of them.
Kayla Evans
Kayla Evans el 13 de Jul. de 2020
The code in the question with the for loop was just an example of the distance formula. The full for loop that I have in the program is:
abc = size(tpoint);
xycoord = [];
for m = 1:abc(1)
dist = sqrt((tpoint(m,1)-xyints(1:end,1)).^2+(tpoint(m,2)-xyints(1:end,2)).^2);
%find the distance under threshold and use that as an index into xyints:
coord = xyints(dist<3,:);
xcoord = coord(:,1);
ycoord = coord(:,2);
xycoord = cat(1,xycoord,[xcoord,ycoord]);
end
I have this loop inside a function, and I'm plotting the coordinates outside of the function. It appears the be working with the convention that I'm currently using.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 13 de Jul. de 2020
Editada: Image Analyst el 13 de Jul. de 2020
Use pdist2() in the Statistics and Machine Learning Toolbox (if you have it):
tpoint = [1 2
4 5
7 8
10 11];
xyints = [12 16
2 18
24 19];
distances = pdist2(tpoint, xyints)
[rows, columns] = find(distances < 12)
for k = 1 : length(rows)
fprintf('Point #%d of tpoint and point #%d of xyints are %.2f apart.\n',...
rows(k), columns(k), distances(rows(k), columns(k)));
% Draw a line
x = [tpoint(rows(k), 1), xyints(columns(k), 1)];
y = [tpoint(rows(k), 2), xyints(columns(k), 2)];
plot(x, y, 'g-', 'LineWidth', 2);
end
distances =
17.8044938147649 16.0312195418814 28.6006992921502
13.6014705087354 13.1529464379659 24.4131112314674
9.4339811320566 11.1803398874989 20.2484567313166
5.3851648071345 10.6301458127346 16.1245154965971
Point #3 of tpoint and point #1 of xyints are 9.43 apart.
Point #4 of tpoint and point #1 of xyints are 5.39 apart.
Point #3 of tpoint and point #2 of xyints are 11.18 apart.
Point #4 of tpoint and point #2 of xyints are 10.63 apart.

Categorías

Más información sobre Dimensionality Reduction and Feature Extraction en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by