2012-11-03 175 views
0

我不斷收到此錯誤:Matlab「索引超出矩陣尺寸」。

??? Index exceeds matrix dimensions.

Error in ==> example3_6 at 48 results=regress(cl1(trainset), cl2(trainset)); % use regression function

GDX文件包含7列和386列,GLD文件包含7列和765行,但如果我是在250樣本出來的兩個這不應該是一個問題。

有人可以提出這裏有什麼問題嗎?

謝謝

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

爲什麼被標記爲C++? –

回答

0

它看起來像你的電話到迴歸功能相關的問題。

根據DOC迴歸:

迴歸(Y,X)返回係數估計的p×1矢量b用於在X,X的預測值y中的響應的多線性迴歸是n p個預測變量在n個觀測值中的每一個的p矩陣。 y是觀察到的響應的n乘1矢量。

但是由於我們不知道如何在ols函數中使用迴歸函數,所以很難爲您提供幫助。只需對發送給regress()的兩個參數進行尺寸檢查(...),我相信你會發現MATLAB爲什麼抱怨。

+0

我跑的大小()函數,這就是我得到 大小(CL1)ANS = 2 1 大小(小火車),ANS = 1 252 我敢肯定,錯誤是因爲我堵1: 252向量轉換爲2:1矩陣,但是如何改變2:1矩陣大小以適應向量?這是我第一次使用matlab,所以我不是很熟悉它。只是用它來跟着我正在閱讀的書。 –