what about
diff__ = bsxfun(@minus,repmat(A,N,1),A');
which can be definitely improved by doing
diff__ = bsxfun(@minus,A,A');
?
A little performance check:
N = 1000;
v = rand(N,1);
tic
diff__ = bsxfun(@minus,repmat(v,N,1),v');
toc
tic
diff__ = bsxfun(@minus,v,v');
toc
result
Elapsed time is 105.343344 seconds.
Elapsed time is 1.124946 seconds.
(Tim's data check:
diff__ =
0 2 6 4
-2 0 4 2
-6 -4 0 -2
-4 -2 2 0
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…