我有这样的x,y点列表
points = [[1,2];[2,5];[7,1]...[x,y]]
如何过滤点数组以仅返回符合条件的点
例如,返回0 我有这个但它给出了一个错误。 任何帮助将不胜感激!points(points(1,:) > 0 && points(1,:) < 5 , points(:,1) > 0 && points(:,1) < 2)
您的代码有几个问题:
&&
替换为&
。区别在于&&
仅用于标量,并且会短路(即,即使0 && x
未定义,x
仍然有效)。points(:,1)
和points(:,2)
。索引也不正确。逻辑条件的结果是一个逻辑索引,应该仅在第一维上应用(选择某些行),而在第二维中使用:
(保留所有列):
points(points(:,1) > 0 & points(:,1) < 5 & points(:,2) > 0 & points(:,2) < 2, :)
作为旁注,不需要points
定义中的内括号:
points = [1,2 ;2,5; 7,1];