Why do I get the error “Index exceeds matrix dimensions.”?
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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;
1 comentario
Respuestas (6)
Nagham Kabbara
el 15 de Nov. de 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!!!!!
0 comentarios
John
el 3 de Nov. de 2012
Editada: John
el 3 de Nov. de 2012
1 comentario
Matt J
el 3 de Nov. de 2012
Editada: Matt J
el 3 de Nov. de 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.
John
el 3 de Nov. de 2012
2 comentarios
Tim
el 19 de Dic. de 2012
I am getting the same error, any chance you could share the date formatting problem and solution?
Tim
el 21 de Dic. de 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.
MathWorks Support Team
el 27 de Nov. de 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:
0 comentarios
veerendra babu arigela
el 14 de Mzo. de 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"
0 comentarios
Ver también
Categorías
Más información sobre Time Series Events en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!