Hello,

I have a self-defined function to be computed on each column in a matrix. 
The basic idea is to ignore the elements that have value of 0 during
computation. 

I should be able to write my own function but it could be computational
expensive, so I'd love to ask if anyone may have suggestions on how to
implement it more efficiently.  Thanks in advance. 

For example, there are three vectors in the matrix, which are
A       B       C
1       0       1
-1      1       1
-1      -1      1
1       0       -1
        
Distance(AB) = (-1X1+(-1)X(-1))/de(AB) , and 
de(AB) = sqrt(square(-1)+square(-1)) X sqrt(square(1)+square(-1))

Distance(BC) = (1X1+(-1)X1)/de(BC) ,and 
de(BC) = sqrt(square(1)+square(-1)) X sqrt(square(1)+square(1))

Distance(AC) = (1X1+(-1)X1+(-1)X1+1X(-1))/de(AC), and 
de(BC) = sqrt(square(1)+square(-1)+square(-1)+square(1)) X
sqrt(square(1)+square(1)+square(1)+square(-1))

As you may see, the numerator is basically the dot product of the two
vectors; this function actually is more like the cosine function in R, but
with some variations.

I would need to compute the distance between any two vectors in a matrix. 
It would be ideal if the results could be the output that produces by some R
distance function.  

Thanks.







--
View this message in context: 
http://r.789695.n4.nabble.com/self-defined-distance-function-to-be-computed-on-matrix-tp4641860.html
Sent from the R help mailing list archive at Nabble.com.
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to