在 MATLAB 中,出现“数组索引必须为正整数或逻辑值”错误通常是由于数组索引不是有效的整数造成的。

例如,以下代码会在第 51 行出现错误:

P_out(i) = P_out(i-1) + delta_P + Kp*error;

错误信息表明 i 变量可能不是整数。为了修复这个错误,您可以尝试以下两种方法:

  1. 使用 int32 函数将 i 转换为整数:
P_out(int32(i)) = P_out(int32(i-1)) + delta_P + Kp*error;
  1. 使用 round 函数将 i 四舍五入为最接近的整数:
P_out(round(i)) = P_out(round(i-1)) + delta_P + Kp*error;

通过将 i 转换为整数或四舍五入,您可以确保数组索引始终是有效的整数,从而解决此错误。

MATLAB 错误:数组索引必须为正整数或逻辑值 - 如何修复

原文地址: https://www.cveoy.top/t/topic/o5il 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录