This is 10x times than arrayfun, still i think can be optimsed.
Because no pre-allocation in below code and many other potential improvements.
function [subRow, subCol] = elementwisesubscript(rowStart, rowEnd, colStart, colEnd)
%% subRow identification
rowLength = rowEnd-rowStart+1;
maxRowLength = max(rowLength);
subRow = repmat(rowStart, 1, maxRowLength) + repmat(0:maxRowLength-1, length(rowStart),1);
colLength = colEnd-colStart+1;
for i =2:maxRowLength
subRow(rowLength<i, i) = 0;
end
subRow = subRow';
subRow = subRow(:);
subRow = repelem(subRow, repelem(colLength, maxRowLength));
subRow(subRow==0)=[];
%% subCol identification
maxColLength = max(colLength);
subCol = repmat(colStart, 1, maxColLength) + repmat(0:maxColLength-1, length(colStart),1);
for i =2:maxColLength
subCol(colLength<i, i) = 0;
end
subCol = subCol(:);
colLengthMat = repmat(rowLength,1,maxColLength);
colLengthMat = colLengthMat(:);
subCol = repelem(subCol, colLengthMat);
subCol = reshape(subCol, [], maxColLength);
subCol = subCol';
subCol = subCol(:);
subCol(subCol==0)=[];
end