Why do I get the error “Index exceeds matrix dimensions.”?

43 views (last 30 days)
I keep getting this error:
??? Index exceeds matrix dimensions.
Error in ==> example3_6 at 48 results=regress(cl1(trainset), cl2(trainset)); % use regression function
GDX file contains 7 columns and 386 rows, GLD file contains 7 columns and 765 rows, but if i'm taking a sample of 250 out of both this should not be a problem.
Can somebody advise what is the problem here?
Thank you
clear; % make sure previously defined variables are erased.
[num, txt]=xlsread('GLD'); % read a spreadsheet named "GLD.xls" into MATLAB.
tday1=txt(2:end, 1); % the first column (starting from the second row) is the trading days in format mm/dd/yyyy.
tday1=datestr(datenum(tday1,'mm/dd/yyyy'), 'yyyymmdd');
tday1=str2double(cellstr(tday1)); % convert the date strings first into cell arrays and then into numeric format.
adjcls1=num(:, end); % the last column contains the adjusted close prices.
[num, txt]=xlsread('GDX'); % read a spreadsheet named "GDX.xls" into MATLAB.
tday2=txt(2:end, 1); % the first column (starting from the second row) is the trading days in format mm/dd/yyyy.
tday2=datestr(datenum(tday2,'mm/dd/yyyy'), 'yyyymmdd');
tday2=str2double(cellstr(tday2)); % convert the date strings first into cell arrays and then into numeric format.
adjcls2=num(:, end); % the last column contains the adjusted close prices.
[tday, idx1, idx2]=intersect(tday1, tday2); % find the intersection of the two data sets, and sort them in ascending order
cl1=adjcls1(idx1);
cl2=adjcls2(idx2);
trainset=1:252; % define indices for training set
testset=trainset(end)+1:length(tday); % define indices for test set
% determines the hedge ratio on the trainset
results=ols(cl1(trainset), cl2(trainset)); % use regression function
hedgeRatio=results.beta;

Answers (6)

Matt J
Matt J on 3 Nov 2012
Edited: Matt J on 3 Nov 2012
Some indices contained in "trainset" are outside the bounds of legitimate indices for cl1 or cl. I.e., you've done something like this:
>> x=1:5; x(6)
Index exceeds matrix dimensions.

Nagham Kabbara
Nagham Kabbara on 15 Nov 2015
i know this is an old post, but i am having the same problem but the difference is that i know the size of the matrix and i'm setting a loop for i=1:nbrows and still i am getting this message!!!
this is so illogical!!!!!

John
John on 3 Nov 2012
Edited: John on 3 Nov 2012
I ran the size() function and this is what i get
size(cl1) ans = 2 1
size(trainset) ans = 1 252
I'm sure the error is because i'm plugging a 1:252 vector into a 2:1 matrix, but how do i change the 2:1 matrix size to fit the vector in? This is the first time i'm using matlab, so i'm not really familiar with it.
  1 Comment
Matt J
Matt J on 3 Nov 2012
Edited: Matt J on 3 Nov 2012
No, that's not the reason. As you can see from the following example, there is no reason trainset can't be longer than cl.
>> cl=[ 5; 6]; trainset=ones(1,252); z=cl(trainset);
>> whos cl trainset z
Name Size Bytes Class Attributes
cl 2x1 16 double
trainset 1x252 2016 double
z 252x1 2016 double
You need to look at max(trainset) and see if it's greater than length(cl). Or, if you know that cl is too short in length, then review how cl1 is generated. Only you can possibly know the purpose cl1 is supposed to serve here and how long it should be if it is doing the correct thing.

Sign in to comment.


John
John on 3 Nov 2012
ok i figured it out, there was a problem in date format
  2 Comments
Tim
Tim on 19 Dec 2012
I am getting the same error, any chance you could share the date formatting problem and solution?
Tim
Tim on 21 Dec 2012
Nevermind, my problem was using the MATLAB function names, "year" and "month", as my looping variables. Just changed them to 'yr' and 'mo' and my code ran fine. Thanks anyway.

Sign in to comment.


MathWorks Support Team
MathWorks Support Team on 27 Nov 2018
Typically you encounter this error when you try to access an element of an array that does not exist. For example, in R2017b and earlier releases:
A = [1 7 9 3];
A(5)
, results in this error because you are trying to access the fifth element of A, which does not exist.
In R2018a and later, this error is replaced by the following error:
"Index exceeds the number of array elements (4)."
For additional information on indexing, see:

veerendra babu arigela
veerendra babu arigela on 14 Mar 2019
Warning: Could not evaluate MaskDisplay commands of block 'veeru/Diode':
Undefined variable "X" or class "X.p1".
> In OutputMCback
In powericon
In sps_rtmsupport
pls help me it is showing "index exceeds matrix dimension"

Tags

Products

Community Treasure Hunt

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

Start Hunting!