程序报错下标索引必须为正整数类型或逻辑类型。

% 更新接收器收到的光强
r_idx = floor(sqrt(x^2 + y^2) / dr) + 1;
z_idx = floor(z / dz) + 1;
if r_idx <= length(r_bins) && z_idx <= length(z_bins)
received_power(z_idx, r_idx) = received_power(z_idx, r_idx) + I;
end
报错信息:下标索引必须为正整数类型或逻辑类型
出错 jg (line 37)
received_power(z_idx, r_idx) = received_power(z_idx, r_idx) + I;
求助这是什么情况

 采纳的回答

firqang
firqang 2023-5-14

0 个投票

try里面要放出错语句,否则catch是抓不到的。
建议按照下面示例写:
部分代码示例.....
try
received_power(z_idx, r_idx) = received_power(z_idx, r_idx) + I;
catch
z_idx
r_idx
end
这样的话,在函数报出上面错误的时候,就会进入catch分支,然后就会把z_idx, r_idx打印到命令窗口。
你就能知道出错时候,这两个值是什么了。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!