Srishti M
Srishti M

Reputation: 543

Error: Index exceeds matrix dimension in simulating AR process

I have a data file of size 256 rows. I want to fit the data to a second order AR(2) process and then simulate the process after fitting. I have the following code but it returns error Index exceeds matrix dimensions.

whos y1
  Name      Size            Bytes  Class    Attributes

  y1        1x1              1712  cell  

whos coeff

is  1x3                24  double              

Please help in resolving the error. Also, how do I check with a plot that the original data and the fitted data are almost same and get the error ?

load('b1.dat');
y1=b1(:);

if ~iscell(y1); y1 = {y1}; end

model = ar(y1, 2, 'ls');



coeffs = model.a;
ar_coeff1=[coeff(2) coeff(3)]


%simulate
for i =3 : 256

   y1(i) = coeff(2) *y1(i-1) +coeff(3)*y1(i-2) ;  **% This line returns error**

end

Upvotes: 1

Views: 467

Answers (1)

Buck Thorn
Buck Thorn

Reputation: 5073

The indexing operation, cell addition, and cell/double multiplication operations as posted are not allowed.

If a is a cell array (such as y1) generated as follows:

>> a={1:256}
a = 

    [1x256 double]

>> whos a
  Name      Size                    Bytes  Class

  a        1x1                      2108  cell array

Grand total is 257 elements using 2108 bytes

I cannot index into a(2) because it doesn't exist:

>> a(2)
??? Index exceeds matrix dimensions.

I cannot add one cell and another as follows:

>> a(1)+a(1)
??? Function 'plus' is not defined for values of class 'cell'.

and I cannot multiply a cell and type double as follows:

>> a*3
??? Function 'mtimes' is not defined for values of class 'cell'.

Error in ==> mtimes at 16
  builtin('mtimes', varargin{:});

As an example, the following is allowed:

for ii =3 : 256
   y1{1}(ii) = coeff(2) *y1{1}(ii-1) +coeff(3)*y1{1}(ii-2) ;  
end

Note also from the ARFIT demo file:

% ... ARfit contains modules for estimating parameters of AR models from given time series data; for checking the adequacy of an estimated AR model; ...

so check the ARfit documentation and demo.

Edit:

In general if ydat is the source data (in cell array format) and ysim is the result of a fit to the data (or modeling operation), then one can plot the residuals between the data and fit with plot(ydat{1}-ysim{1}) and compute the RMS deviation as sqrt(sum(ydat{1}-ysim{1}).^2)/length(ysim{1})

Upvotes: 2

Related Questions