Signed distance matrix in R
问题 [Note, I wrote out this question, then found an answer. I thought maybe someone else would like to know it, so I'm posting the answer just in case. I'm not sure if this is the "done thing"]. Suppose I want the signed distance matrix of a vector, i.e. the distance's aren't always positive, but can be negative. You can't use dist() because it returns absolute values. 回答1: Here's another approach, which is much faster and needs less memory: y <- sample (1 : 4) distmat <- outer (y, y, `-`) yields