I have a matrix mat
that looks like:
A B C D E F
1 0.74 1.19 0.01 1.21 16000 0.02
2 0.76 1.17 0.01 1.21 15500 0.02
3 0.79 1.16 0.01 1.17 15625 0.02
4 0.75 1.17 0.01 1.17 15600 0.02
5 0.80 1.19 0.01 1.19 15225 0.02
6 0.79 1.18 0.01 1.18 15625 0.02
And I want to build a Symmetric matrix from this by applying the function Sum(Col1-Col2)
. The end result will look something like this:
A B C D E F
A 0
B 0
C 0
D 0
E 0
F 0
Such that the blank spaces represent the sum of difference. i.e. [1,2] = Sum(A-B)
.
I have investigated methods such as:
combs<-combn(names(mat),2)
val<-apply(combs,2,function(x) mat[[x[1]]]-mat[[x[2]]])
But it doesn't give me a nice symmetric matrix.
Anyone have any ideas?
Thanks.
EDIT - Thanks to Troy the above works. But how about if I want to compute Sum((Col1-Col2)^2)
in that Sum(((A_1,A_2,..,A_n)-(B_1,B_2,..,B_n))^2)
(so can't sum A
and B
initially and then subtract otherwise the answer will be off).