Problems with intersect function

10 次查看(过去 30 天)
Nithin
Nithin 2011-10-20
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 个评论
Fangjun Jiang
Fangjun Jiang 2011-10-24
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

请先登录,再进行评论。

回答(1 个)

Sean de Wolski
Sean de Wolski 2011-10-20
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 个评论
Nithin
Nithin 2011-10-24
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
Sean de Wolski
Sean de Wolski 2011-10-25
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by