Hello R-list. I have three vectors a, b, c with different lengths and I want to count the number of differences between a[i], b[j], c[k] where a[i], b[j], c[k] distinct. That is, the number of pairs (i, j) with a[i]-b[j] =u or the numbers of triples (i, j, k) with a[i]-b[j] =u and a[i]-c[k] =v. For the first case I use the following commands: dif.ab <- matrix(0, nrow=length(b), ncol=length(a)) for (i in 1:length(b) { dif.ab[i,] <- a-b[i] } dif.ab <- as.vector(dif.ab) dif.ab <- dif.ab[dif.ab !=0] Cab <- vector(mode="integer", length=nu) for ( u in 1:nu ) { Cab[u] <- sum( abs(dif.ab-u)<h ) } or dif.ab <- matrix(0, nrow=length(b), ncol=length(a)) for (i in 1:lengt(a)) { for (j in 1:length(b)) { if (a[i] != b[j]) dif[j,i] <- a[i]-b[j] }} Cab <- vector(mode="numeric", length=nu) for (u in 1:nu) { Cab[u] <- sum(abs(dif-u)<h) } Notice that I use large data sets (length a, b, c= from 300 to 1500) and seek the fastest way to do it especially for the 3D case. I am waiting for your suggestions. Thank you in advance. [[alternative HTML version deleted]]