Problems with intersect function

9 visualizaciones (últimos 30 días)
Nithin
Nithin el 20 de Oct. de 2011
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 comentario
Fangjun Jiang
Fangjun Jiang el 24 de Oct. de 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

Iniciar sesión para comentar.

Respuestas (1)

Sean de Wolski
Sean de Wolski el 20 de Oct. de 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 comentarios
Nithin
Nithin el 24 de Oct. de 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
Sean de Wolski
Sean de Wolski el 25 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by