简体   繁体   English

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

[英]Matlab “Index exceeds matrix dimensions.”

I keep getting this error: 我不断收到此错误:

??? ??? Index exceeds matrix dimensions. 索引超出矩阵尺寸。

Error in ==> example3_6 at 48 results=regress(cl1(trainset), cl2(trainset)); 错误==> example3_6在48结果=回归(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. GDX文件包含7列和386行,GLD文件包含7列和765行,但是如果我要从这两者中抽取250个样本,这应该不是问题。

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;

It looks like a problem with your call to the regress function. 您对回归函数的调用似乎有问题。

According to doc regress: 根据文档回归:

regress(y,X) returns a p-by-1 vector b of coefficient estimates for a multilinear regression of the responses in y on the predictors in X. X is an n-by-p matrix of p predictors at each of n observations. regress(y,X)返回系数估计值的p比1向量b,用于对x中的预测变量上y的响应进行多线性回归。X是n个观测值中每个p预测变量的n-p矩阵。 y is an n-by-1 vector of observed responses. y是观察到的响应的n×1向量。

But since we don't know how you use the regress function in your ols function, it's pretty hard to help you. 但是,由于我们不知道您如何在ols函数中使用回归函数,因此很难为您提供帮助。 Just run a size(...) check on the two arguments you send to regress() and I'm sure you will find out why MATLAB is complaining. 只需对发送到regress()的两个参数运行一个size(...)检查,我相信您会发现MATLAB为何会抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM