function s = ami_decode(a)n = length(a);s = zeros(1,n);sg = 1;for i = 1:n if a(i) == 0 s(i) = 0; elseif a(i) == 1 if mod(sg,2) == 0 s(i) = 0; else s(i) = 1; sg = sg + 1; end elseif a(i) == -1 if mod(sg,2) == 0 s(i) = 0; else s(i) = -1; sg = sg + 1; end end s(i) = s(i-1);endend MATLAB运行显示下标索引必须为正整数类型或逻辑类型。 出错 s(i)=s(i-1); 如何修改代码
时间: 2024-01-06 15:05:54 浏览: 82
hw_decode_linuxffmpeg_hw_decode_linuxffmpegmp4_vaapi硬件解码_源码
5星 · 资源好评率100%
可以将循环的第一次迭代单独处理,避免在第一次迭代时访问s的负数索引。可以在循环之前将s的第一个元素初始化为0,或者在第一次迭代时将s(i-1)替换为0。以下是修改后的代码:
```
function s = ami_decode(a)
n = length(a);
s = zeros(1,n);
sg = 1;
for i = 1:n
if a(i) == 0
s(i) = 0;
elseif a(i) == 1
if mod(sg,2) == 0
s(i) = 0;
else
s(i) = 1;
sg = sg + 1;
end
elseif a(i) == -1
if mod(sg,2) == 0
s(i) = 0;
else
s(i) = -1;
sg = sg + 1;
end
end
if i > 1
s(i) = s(i-1);
end
end
end
```
阅读全文