【问题标题】:Matlab "Index exceeds matrix dimensions."Matlab“索引超出矩阵维度。”
【发布时间】:2012-11-03 19:17:02
【问题描述】:

我不断收到此错误:

???索引超出矩阵维度。

==> example3_6 在 48 个结果中出错=regress(cl1(trainset), cl2(训练集)); % 使用回归函数

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;

【问题讨论】:

    标签: matlab trading


    【解决方案1】:

    您对回归函数的调用似乎有问题。

    根据文档回归:

    regress(y,X) 返回一个 p×1 向量 b 的系数估计值,用于对 y 中的响应对 X 中的预测变量进行多线性回归。X 是每个位置的 p 个预测变量的 n×p 矩阵n 个观察值。 y 是观察到的响应的 n×1 向量。

    但由于我们不知道您如何在 ols 函数中使用回归函数,因此很难为您提供帮助。只需对您发送给 regress() 的两个参数运行 size(...) 检查,我相信您会发现 MATLAB 抱怨的原因。

    【讨论】:

    • 我运行了 size() 函数,这就是我得到的 size(cl1) ans = 2 1 size(trainset) ans = 1 252 我确定错误是因为我正在插入一个1:252 向量转换为 2:1 矩阵,但如何更改 2:1 矩阵大小以适应向量?这是我第一次使用matlab,所以我不是很熟悉它。只是用它来跟随我正在阅读的书。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    相关资源
    最近更新 更多