在Matlab中过滤2D点数组以符合条件

问题描述 投票:0回答:1

我有这样的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)

任何帮助将不胜感激!

arrays matlab find
1个回答
1
投票

您的代码有几个问题:

  • 您需要将&&替换为&。区别在于&&仅用于标量,并且会短路(即,即使0 && x未定义,x仍然有效)。
  • 在逻辑条件下,您应该分别对xy列使用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];
    
© www.soinside.com 2019 - 2024. All rights reserved.