Calculate all the possibilities of differences in the vector

Let's say I have a short vector x = [a,b,c,d,e];What would be the best way to calculate the whole difference between the members of a vector like:

y = [e-d e-c e-b e-a
     d-e d-c d-b d-a
     c-e c-d c-b c-a
     b-e b-d b-c b-a
     a-e a-d a-c a-b];

Thanks in advance

+3
source share
2 answers

To give this exact matrix try:

x = [1;2;3;4;5];     %# note this is a column vector (matrix of rows in general)

D = squareform( pdist(x,@(p,q)q-p) );
U = triu(D);
L = tril(D);
y = flipud(fliplr( L(:,1:end-1) - U(:,2:end) ))

lead to this case:

y =
     1     2     3     4
    -1     1     2     3
    -2    -1     1     2
    -3    -2    -1     1
    -4    -3    -2    -1
+7
source

First create a circulant matrix , then calculate the difference between the first column and the rest of the columns. Here is a link to create a circulant matrix.

0
source

All Articles