Matlab “Index exceeds matrix dimensions" when I was forming momentum strategy

1 visualizzazione (ultimi 30 giorni)
Hi, guys, I am a fresher in Matlab. I am trying to use matlab to creat my momentum strategy. Unfortunately I always meet index exceeds matrix dimensions error, which makes me confused. the error line has been figured out and I really hope someone could help me. Thank you very much.
the error shown in matlab is "Index exceeds matrix dimensions.
Error in Untitled (line 39) idwinner(k,1:ncandidates) = stockid(idx(1:ncandidates));"
my code is
stockdata=xlsread('j:\NPCSDesktop\data0.xlsx','sheet3','B2:ZK109'
stockid=xlsread('j:\NPCSDesktop\data0.xlsx','sheet3','B1:ZK1')
[totalmonths,totalstocks]=size(stockdata);
period1=[1,3,6,9];
period2=[1,3,6,9];
startmonth=1;
%get the number of stocks for each week
for i=1:totalmonths
nbrstocks(i) = totalstocks - sum(isnan(stockdata(i,:)));
end
for i = 1:length(period1)
for j = 1:length(period2)
for n = 1:2
p1 = period1(i);
p2 = period2(j);
% initialize
Rwinner = zeros(totalmonths,1);
Rloser = zeros(totalmonths,1);
idwinner = zeros(totalmonths,round(max(nbrstocks)*0.1));
idloser = zeros(totalmonths,round(max(nbrstocks)*0.1));
for k = startmonth:totalmonths+1-p1-p2-(n-1)
start1 = k;
stop1 = start1+p1-1;
ordermonths = start1:stop1;
start2 = stop1+1+(n-1);
stop2 = start2+p2-1;
holdmonths = start2:stop2;
nstocks = nbrstocks(k);
% order R for the ordering weeks
data1 = stockdata(ordermonths, 1:nstocks);
R1 = ones(1,nstocks);
for m = 1:length(ordermonths)
R1 = R1.*data1(m,:);
end
R1 = R1-1;
% select for the winners and losers
[B,idx] = sort(R1,'descend');
ncandidates = round(nstocks*0.1);
idwinner(k,1:ncandidates) = stockid(idx(1:ncandidates));
idloser(k,1:ncandidates) = stockid(idx(end-ncandidates+1:end));
% caculate the R for winners and losers in holding weeks
data2winner = stockdata(holdmonths, idx(1:ncandidates));
data2loser = stockdata(holdmonths, idx(end-ncandidates+1:end));
R2w = ones(1,ncandidates);
R2l = ones(1,ncandidates);
for m = 1:length(holdmonths)
R2w = R2w.*data2winner(m,:);
R2l = R2l.*data2loser(m,:); end
R2w = R2w-1;
R2l = R2l - 1;
Rwinner(k) = mean(R2w);
Rloser(k) = mean(R2l);
end
xlswrite(fullfile(['strategy_' num2str(p1) '_' num2str(p2) '_' num2str(n) '.xlsx']),Rwinner,'Rwinner');
xlswrite(fullfile(['strategy_' num2str(p1) '_' num2str(p2) '_' num2str(n) '.xlsx']),Rloser,'Rloser');
xlswrite(fullfile(['strategy_' num2str(p1) '_' num2str(p2) '_' num2str(n) '.xlsx']),idwinner,'idwinner');
xlswrite(fullfile(['strategy_' num2str(p1) '_' num2str(p2) '_' num2str(n) '.xlsx']),idloser,'idloser');
end
end
end

Risposte (1)

Sean de Wolski
Sean de Wolski il 26 Ago 2014
The easiest way to debug this kind of error is with "Stop on Errors". Run the following to turn it on:
>>dbstop if error
Now run what you have above. It will stop on the line that errors and you will be able to inspect the variables to see which index where is exceeding the dimensions.
  3 Commenti
go go
go go il 27 Ago 2014
Modificato: go go il 27 Ago 2014
I found out that the code works well if use a random data. But if I import data from the excel, the error will happen.
there is no error if i use s=-1+(1-(-1)).*rand(715,215); stockdata=transpose(s); stockdata(stockdata > 0.5) = NaN; stockid=[1:715];
Geoff Hayes
Geoff Hayes il 31 Ago 2014
stockid=xlsread('j:\NPCSDesktop\data0.xlsx','sheet3','B1:ZK1')
would return an empty matrix. Solution was to do something more like
[~,stockid]=xlsread('j:\NPCSDesktop\data0.xlsx','sheet3','B1:ZK1');
and set the idwinner and idloser arrays to cell arrays.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by