簡體   English   中英

Matlab“索引超出矩陣尺寸。”

[英]Matlab “Index exceeds matrix dimensions.”

我不斷收到此錯誤:

??? 索引超出矩陣尺寸。

錯誤==> example3_6在48結果=回歸(cl1(trainset),cl2(trainset)); 使用回歸函數的百分比

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;

您對回歸函數的調用似乎有問題。

根據文檔回歸:

regress(y,X)返回系數估計值的p比1向量b,用於對x中的預測變量上y的響應進行多線性回歸。X是n個觀測值中每個p預測變量的n-p矩陣。 y是觀察到的響應的n×1向量。

但是,由於我們不知道您如何在ols函數中使用回歸函數,因此很難為您提供幫助。 只需對發送到regress()的兩個參數運行一個size(...)檢查,我相信您會發現MATLAB為何會抱怨。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM