Problems with intersect function

조회 수: 9 (최근 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