Problems with intersect function

Hi everybody,
I have the following problem. I describe the following lists of points:
x1 = -1.5:h:1.5;
x2 = -1.5:h:1.5;
[x1 x2] = meshgrid(x1,x2);
x1 = x1(:);
x2 = x2(:);
X = [x1 x2];
I want to check wether the point some point (p, q) is part of the list. I do the following:
point = [-0.75, 0.55];
intersect(X, point, 'rows');
Matlab returns me an empty matrix. Whereas I know for certain that point is inside the list. I checked even manually by open X in the workspace!
Does anybody know what is going on. Is this is a bug? I need to perform set intersections for some problem.
regards,
nithin

1 Commento

Due to floating point comarison.
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Accedi per commentare.

Risposte (1)

A fix:
X(all(abs(bsxfun(@minus,X,point))<(10^-10),2),:)
Row with both absolute differences less than tolerance. Will only work as written for one point. That could be changed using the third dimension.
MORE
point = your_points;
X = your_matrix;
sz = size(X,1);
inpts = cell(sz,1)
for ii = 1:sz
inpts{ii} = expression_above_of_points(ii)
end
intersected_points = unique(cell2mat(inpts),'rows')
unique won't have the floating point issues since they'll all be extracted from X and thus actually equal.

5 Commenti

Nithin
Nithin il 20 Ott 2011
Thanks!
Nithin
Nithin il 20 Ott 2011
Hmm the next problem: I indeed want to do it for more than one point. Infact I need to do it for a large amount of points. Using repmat to elongate X in the third dimension is simply not feasible. It takes to much resources. How to get out of this one?
bsxfun with point permuted into the third dimension. Or just run a for-loop iterating through your list of points storing the results at the end of each.
Nithin
Nithin il 24 Ott 2011
Hi sean,
That is what I did, extend it to the third dimension. It works for a few points, but when you have a lot of points, making the 3D matrix takes too much time (I used repmat to copy X in the 3rd dimension).
regards,
nithin
repmat is the wrong approach since it'll require creating that huge array twice (one too many times). permute and/or reshape is what you should be looking for, however, you'll still need that big matrix once.
I would just use a for-loop. See the edit it my above post.

Accedi per commentare.

Richiesto:

il 20 Ott 2011

Community Treasure Hunt

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

Start Hunting!

Translated by