计算特定半径内连续点的Matlab代码

2024-04-26 11:50:51 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:给定两个平行列表xy,计算索引i给定点的特定半径R内连续点的数量。我可以用Python实现这一点,但在转换到Matlab时遇到一些困难

以下是我用Python编写的代码:

R = 0.3
num_pts = 0
j = i
while j >= 0 and sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2) <= R:
  num_pts += 1
  j -= 1
j = i + 1
while j < len(x) and sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2) <= R:
  num_pts += 1
  j += 1

变量num_pts现在有了我们想要的答案,但是下面的Matlab代码不会这样做:

R = 0.3
num_pts = 0
j = i
while ( (j >= 1) & (sqrt(((x(i) - x(j))^2) + ((y(i) - y(j))^2)) <= R) )
  num_pts = num_pts + 1;
  j = j - 1; 
end

j = i + 1;

while ((j <= numel(x)) & (sqrt((x(i) - x(j))^2 + (y(i) - y(j))^2) <= R))
  num_pts = num_pts + 1;
  j = j + 1;
end

任何有帮助的,提前谢谢

编辑:问题解决了。上面的代码可以工作(如果它不尝试在MATLAB中使用&;而不是&;)


Tags: and代码列表数量len半径sqrtnum