Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract all vector pairs

Tags:

r

matrix

bigdata

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?

like image 203
ari6739 Avatar asked Sep 20 '25 13:09

ari6739


1 Answers

Perhaps use combn

combn(row.names(m1), 2, function(x) m1[x[1],] - m1[x[2],])
like image 105
akrun Avatar answered Sep 22 '25 07:09

akrun