1

我不断收到此错误:

索引表达式超出范围。试图访问元素 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
4

0 回答 0