Problems with intersect function
8 views (last 30 days)
Show older comments
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 Comment
Fangjun Jiang
on 24 Oct 2011
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
Answers (1)
Sean de Wolski
on 20 Oct 2011
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 Comments
Sean de Wolski
on 25 Oct 2011
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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!