I have a matrix of size 2000 X 700.
I want to subtract all possible pairs of rows.
If x_i
represent a row then I want to compute: x_1-x_2, x_1-x_3, ..., x_2-x_3,...
Example:
mat
1 2 3
5 3 2
1 1 6
My output should be
x_1 - x_2: -4 -1 1
x_1 - x_3: 0 1 -3
x_2 - x_3: 4 2 -4
I tried using a loop, but it takes too long. Is there an efficient way to compute this?
Perhaps use combn
combn(row.names(m1), 2, function(x) m1[x[1],] - m1[x[2],])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With