필터 지우기
필터 지우기

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

조회 수: 13 (최근 30일)
John
John 2012년 11월 3일
답변: veerendra babu arigela 2019년 3월 14일
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;

답변 (6개)

Matt J
Matt J 2012년 11월 3일
편집: Matt J 2012년 11월 3일
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 2015년 11월 15일
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 2012년 11월 3일
편집: John 2012년 11월 3일
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
Matt J
Matt J 2012년 11월 3일
편집: Matt J 2012년 11월 3일
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
John 2012년 11월 3일
ok i figured it out, there was a problem in date format
  댓글 수: 2
Tim
Tim 2012년 12월 19일
I am getting the same error, any chance you could share the date formatting problem and solution?
Tim
Tim 2012년 12월 21일
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
MathWorks Support Team 2018년 11월 27일
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 2019년 3월 14일
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"

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by