Momentum Strategy Error - Index exceeds matrix dimensions..?

1 view (last 30 days)
Hi guys !! I'm working on my master thesis and i really have to solve this algorithm and i'm running out of time. My work cinsists on calculating the Jegadeesh and Titman momentum strategy. I have the matlab code but when i execute it an error shows up "Index exceeds matrix dimensions" at the "idwinner(k,1:ncandidates) = stockid(idx(1:ncandidates));" line. I'm new to Matlab and i dont really know what is wrong with this code.
The code:
>> % load stockdata; stockdata = xlsread('data.xlsx','final','B2:S97'); stockid= xlsread('data.xlsx','final','B1:S1');
[totalmonths,totalstocks] = size(stockdata);
period1 = [6];
period2 = [6];
startmonth = 14;
%get the number of stocks for each month
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
Notice that when i enter a random matlab values the code works very well. Instead of extracting values from the Excel worksheet, i proceed like this:
>> % load stockdata; r=-1+(1-(-1)).*rand(715,215); stockdata=transpose(r); stockdata(stockdata > 0.5) = NaN; stockid=[1:715]; [totalmonths,totalstocks] = size(stockdata); period1 = [6]; period2 = [6]; startmonth = 14; .... Help plz
  1 Comment
go go
go go on 27 Aug 2014
Edited: go go on 27 Aug 2014
I met exactly the same problem. Bying usingthe same code I meet the same problem. This error confused me many many days. Have you solved your problem?If you have, could you tell me how to solve the error? I am quite worried about my thesis because the deadline is around the corner. Thank you.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 27 Aug 2014
The error, Index exceeds matrix dimensions, generally means that the code is attempting to access an element in a matrix (or an array) with an index that is larger than the dimension being used for that index. For example,
% A is a 12x2 array
A = zeros(12,2);
% throws error because 3 is greater than the second dimension size of 2
A(12,3)
Since your error is being generated/thrown from the line
idwinner(k,1:ncandidates) = stockid(idx(1:ncandidates));
there is either a problem with idx or with stockid. If you step through the code (with the debugger) you will observe that stockid is an empty matrix. This local variable is initialized at the second line as
stockid= xlsread('data.xlsx','final','B1:S1');
If you open the Excel spreadsheet, you will see that there is the row from B1 to S1, but it is non-numeric i.e. MSFT, XOM, WMT, etc.
According to xlsread,
num = xlsread(filename) reads data from the first worksheet in the Microsoft® Excel® spreadsheet file named filename and returns the numeric data in array num.
So only numeric data is read in. Since there is none in this row, then an empty matrix is returned.
To get the text, just replace the above line with
[~,stockid] = xlsread('data.xlsx','final','B1:S1');
Now stockid will be populated with the non-numeric stock ids. However, this will require you to change how the idwinner and idloser arrays are initialized. Since the stock ids are varying length strings, these two arrays need to be initialized as cell arrays
idwinner = cell(totalmonths,round(max(nbrstocks)*0.1));
idloser = cell(totalmonths,round(max(nbrstocks)*0.1));
Try the above and see what happens!
  5 Comments
Geoff Hayes
Geoff Hayes on 28 Aug 2014
go go - this should be posted as a new question as it does not concern socios's question. In this question, please attach only the relevant files, and describe what you mean by the figure...is very strange and the result is not a common return.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!