It's not exactly the most elegant of codes but I'm still a beginner!
Just change the code in
1) Load data section
2) Input section - no of stocks and no of long positions to take at a time
3) Create as many variables in the 'individual stock return, position and long return section
% Simple Stock Rotation Model
% Ranks stocks based on previous period return and goes long on 'LongCutOff' number of stocks
%Rank Stocks According to Strength over Last Month
[s x]=sort(LDataDiff,2,'descend'); % Sorts it matlab style
[J I]=ndgrid(1:size(x,1),1:size(x,2));% creates a grid from 1:c & 1:r
x(sub2ind(size(x),J,x))=I;
clear s J I
% First row of position is a NaN
x(1,: ) = NaN;
%Lag Position by one period
Position = lagmatrix(x,1);