我一直收到这个错误:
索引表达式超出范围。试图访问元素2 有效范围是1-1。“2”。
我想要的输出是
abs(I(2,ii(a))-I(2,ii(a)-1)
因此,当ii(a)=2
时,我希望I(2,2)
减去I(2,1)
,即5.1减5.0
错误是由于这一行:
arr(1,ii(a)) = arr(1,ii(a)) + abs(I(2,ii(a))-I(2,ii(a)-1));.
我在.mat文件中将I定义为以下内容:
save data -v7.3 'I'
load('data.mat','I');
I =
Columns 1 through 6
0 1.0000 2.0000 3.0000 4.0000
5.0000 5.1000 5.2000 5.2000 5.5000
代码:
function arr= fcn(I,count,oldval)
persistent integ_signal
if isempty( integ_signal)
integ_signal=zeros(1,5)
end
persistent a
if isempty(a)
a=zeros(1)
end
arr=zeros(1,5);
ii=zeros(1,5);
aa=zeros(2,5);
integ_signal=oldval;
coder.extrinsic('load');
aa=load('data.mat','I');
if count==1
a=1;
ii(a)=count;
integ_signal(1,ii(a)) = 10; % Initial Condition
end
if count ~= 1
a=count;
ii(a)=count;
integ_signal(1,ii(a))= integ_signal(1, ii(a)-1);
arr(1,ii(a)) = integ_signal(1,ii(a));
arr(1,ii(a)) = arr(1,ii(a)) + abs(aa(2,ii(a))-aa(2,ii(a)-1));
end
a = a + 1;
end