search for: nonvectorized

Displaying 1 result from an estimated 1 matches for "nonvectorized".

2004 May 25
2
vectorize an expression
...Euclidean distance is a good example but I have other metrics I want to use as well). I want a 5-by-5 array with the [i,j]th element being the distance from a[i,] to a[j,] To Wit: a <- matrix(1:10,5,2) array <- matrix(NA, 5, 5) dist <- function(x1,x2){sqrt(sum(x1-x2)^2)} #NONVECTORIZED BIT FOLLOWS for(i in 1:5) { for(j in 1:5) { array[i,j] <- dist(a[i,] , a[j,]) } } (note that array[i,i]=0 for i=1:5 as expected). How to vectorize this? -- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre SO14 3ZH tel +44(0)23-8059-7743 initialDOTsurna...