Searching for an ordered pair plus or minus some tolerance in an array

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)

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

Oh this is clever. I am currently playing with something like this:
jc = ismember(round(centers),[499,204]);
jclogic = and(jc(:,1),jc(:,2));
jcnotpresent = sum(jclogic)
But the issue is that ismember does not easily allow a tolerance value. Newer versions of Matlab have a function called ismembertol, which would do the job for me. But I am running v2009 and it doesn't seem to have this tool. I think I may have to try your trick, unless you can think of something else . . .
I suspect between your suggestion and a bit of the above code I can come up with something.
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.
I think Euclidean distance should be fine. By tolerance I mean it is within the ideal value plus or minus a few percent. In short, I am looking for bright spots within an image and trying to find the bright spot centers. I then want to search the bright spot centers and determine if certain centers are present or not. Because the camera moves slightly, the image centers may be off by a few percent on any given day.
The code I gave should be perfect for that.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 16 Gen 2016

Commentato:

il 16 Gen 2016

Community Treasure Hunt

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

Start Hunting!

Translated by