Timeline for Compute a cosine dissimilarity matrix in R
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 12, 2023 at 12:47 | comment | added | WJH | Why? Does it make any difference? | |
Aug 25, 2021 at 9:02 | comment | added | Marcin |
it should be sqrt(sum(A^2))*sqrt(sum(B^2)) instead of sqrt(sum(A^2)*sum(B^2))
|
|
Jan 11, 2015 at 17:23 | comment | added | Chiraz BenAbdelkader | Apparently I don't have enough points to be able to comment. I just wanted to offer a slightly modified version of Macro's nice answer. Here it is. # ChirazB's version of cos.sim() by Macro # where S = X %*% t(X) cos.sim.2 <- function(S,ix) { i <- ix[1] j <- ix[2] return( S[i,j]/sqrt(S[i,i]*S[j,j]) ) } #test X <- matrix(rnorm(20),nrow=5,ncol=4) S <- X%*%t(X) n <- nrow(X) idx.arr <- expand.grid(i=1:n, j=1:n) C <- matrix(apply(idx.arr,1,cos.sim,X),n,n) C2 <- matrix(apply(idx.arr,1,cos.sim.2,S),n,n) I don't like global variable, that's why I included S as a parameter. | |
Jul 7, 2012 at 10:20 | comment | added | Greg Slodkowicz | Great, thanks to your reply and ttnphns's comment I was able to do what I want. Now I would like to have a different metric when clustering rows than when clustering columns but maybe that's pushing it... | |
Jul 7, 2012 at 10:19 | vote | accept | Greg Slodkowicz | ||
Jul 5, 2012 at 13:40 | history | edited | Macro | CC BY-SA 3.0 |
missing parentheses in code.
|
Jul 5, 2012 at 12:26 | comment | added | Macro | @GregSlodkowicz, OK well perhaps you can pass this matrix to the function you're using. Also, if you've found this answer helpful please consider an upvote (or accepting the answer if you consider it definitive) :) | |
Jul 5, 2012 at 12:01 | comment | added | Greg Slodkowicz | Thanks, this is helpful. Actually, I don't want to plot the matrix itself but rather have a distance function for clustering of another heatmap that I have. | |
Jul 3, 2012 at 14:33 | history | answered | Macro | CC BY-SA 3.0 |