# 低方差重采样中 weight(i) 越界怎么办

> 原文链接：https://robotics.hellobike.com/pitfalls/2015-07-20-robotics-se-7705
> 频道：pitfalls | 更新日期：2015-07-20 | 标签：auto, stackexchange, mobile-robot, algorithm, particle-filter, probability
> 核心摘要：MATLAB 粒子滤波低方差重采样因 r 取值范围错误导致 weight(i) 越界，改为 0 到 M^-1 的随机数。

# 低方差重采样中 weight(i) 越界怎么办

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[Low variance resampling algorithm for particle filter](https://robotics.stackexchange.com/questions/7705/low-variance-resampling-algorithm-for-particle-filter)（Robotics Stack Exchange #7705）

**状态**：已解决

## 问题

按《Probabilistic Robotics》实现低方差重采样时，`r = rand` 取 `0-1`，循环中访问 `weight(i)` 时 `i` 超过数组维度；提问者考虑循环移位权重数组但不确定是否正确。

## 环境

Linux / ROS 机器人软件与控制通用开发环境

## 解决办法

1. 将 `r` 的取值从 `0-1` 改为 `0` 到 `M^-1`，其中 `M` 对应粒子数或 `state_size`。
2. 在 MATLAB 中可按该范围生成初始随机偏移，例如用 `rand/state_size` 替代 `r = rand`。
3. 不需要通过循环移位 `weight` 数组绕过越界；修正 `r` 后，`U = r + (m - 1)/state_size` 会落在期望的分段概率空间内。

原问题作者：Kelly；采纳答案作者：holmeski。原内容依 CC BY-SA 4.0 授权，本页摘要同样依 CC BY-SA 4.0 授权。
:::

## 原始参考来源 (Sources)

- [Robotics Stack Exchange #7705：Low variance resampling algorithm for particle filter](https://robotics.stackexchange.com/questions/7705/low-variance-resampling-algorithm-for-particle-filter)