bsxfun
can virtually replicate in more than one dimension, allowing you to avoid a loop (sorry naysayers and bitter down-voters, it's true):
P = 50; N = 1000; A = rand(P,N);
% bsxfun for singleton expansion: 50x1000x1 @minus 50x1x1000 => 50x1000x1000
B = bsxfun(@minus,A,permute(A,[1 3 2]));
B = reshape(-B,size(A,1),[]);
That gives a P-by-N*N
matrix which provides all the combinations. To get the thinned out (i.e. with sum(1:N-1)
columns) matrix:
m = logical(tril(ones(N),-1)); % lower triangular logical not including diagonal
B = B(:,m(:));
Finally, concatenate the dates to get what you need:
C = [dates B];