Why do I get the error “Index exceeds matrix dimensions.”?
13 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (6 件)
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!!!!!
0 件のコメント
John
2012 年 11 月 3 日
編集済み: John
2012 年 11 月 3 日
1 件のコメント
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
2012 年 11 月 3 日
2 件のコメント
Tim
2012 年 12 月 19 日
I am getting the same error, any chance you could share the date formatting problem and solution?
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
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:
0 件のコメント
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"
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Time Series Events についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!