######################################################################## # When R adds a number or vector to an array it adds it columnwise # Thus if the 1x3 column mean of a n x 3 array is added to the array # it is added to the 1st 3 entries in col. 1, then the next 3 and so on. # To add to (subtract from) the rows, we need to take the transpose # of the array, add the vector to it, then take the transpose of the result. # The next few functions do this. ######################################################################## add.arr.vec <- function (arr, vec) { # Add a point to an array t(t(arr) + vec) } mult.arr.vec <- function (arr, vec) { # Mult. a point to an array t(t(arr)*vec) } div.arr.vec <- function (arr, vec) { # Divide an array by a point t(t(arr)/vec) } pt.arr.dist <- function(pt, array) { # Compute the Euclidean distance from a point to an array sqrt(apply((t(t(array)-as.vector(pt,mode="numeric")))^2, 1, sum)) }