Searching for an ordered pair plus or minus some tolerance in an array
Mostra commenti meno recenti
Hello. I am trying to search a NX2 array for a particular ordered pair within some tolerance. For example let's say I have an array named 'centers' with the following values:
centers = [...
172.2946 63.6071
178.9731 188.3118
282.7418 150.3119
392.9804 155.2640
498.6229 204.4266
506.5070 75.6916]
I want to search the array 'centers' and return TRUE if a particular ordered pair (within some tolerance of a few percent) is found.
For example if I searched for (199,20) I would get a false or ordered pair not found. But if I searched on (283,149) I would get a true result.
Any suggestions would be most appreciated!
Risposte (2)
Image Analyst
il 16 Gen 2016
Try this:
centers = [...
172.2946 63.6071
178.9731 188.3118
282.7418 150.3119
392.9804 155.2640
498.6229 204.4266
506.5070 75.6916]
p1 = [199,20];
p2 = [283,149];
% Define some distance away that will determine if we return true or false
minAcceptableDistance = 2
% Compute distances of point 1 from each point in centers.
distances = sqrt((centers(:,1)-p1(1)).^2 + sqrt((centers(:, 2)-p1(2)).^2))
% Say if any are within minAcceptableDistance of point 1.
closeEnough = any(distances <= minAcceptableDistance)
% Compute distances of point 2 from each point in centers.
distances = sqrt((centers(:,1)-p2(1)).^2 + sqrt((centers(:, 2)-p2(2)).^2))
% Say if any are within minAcceptableDistance of point 2.
closeEnough = any(distances <= minAcceptableDistance)
4 Commenti
Jose Otero
il 16 Gen 2016
Modificato: Jose Otero
il 16 Gen 2016
Image Analyst
il 16 Gen 2016
I did the Euclidean distance, assuming that your data were points in 2D. If you want some other criteria, such as the City Block Distance, you can do that if you want. But I'm not really clear what your tolerance means. Does it have a physical meaning? If so, show it on a diagram, like scatterplot or something.
Jose Otero
il 16 Gen 2016
Modificato: Jose Otero
il 16 Gen 2016
Image Analyst
il 16 Gen 2016
The code I gave should be perfect for that.
Walter Roberson
il 16 Gen 2016
0 voti
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!