cosine_dist: Calculate cosine-based distances between rows of a matrix

Description Usage Arguments Value Examples

Description

The function cosine_dist takes as its argument an m-by-n matrix x, and returns all cosine-based distances between the rows in that matrix. Rows in x are interpreted as position vectors in n-dimensional space. The cosine-based distance between two rows is based on the angle between these two position vectors, and more precisely is calculated as one minus the cosine of that angle.

Usage

1

Arguments

x

the m-by-n input matrix x.

Value

Returns a square matrix containing the distances between all the rows from the input matrix x.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## first example
m <- matrix(nrow = 3, byrow = TRUE,
            dimnames = list(c('r1','r2','r3'),
                            c('c1','c2','c3')),
            c(.1,.2,.4,
              .2,.2,.6,
              .8,.3, 2))
cosine_dist(m)

## second example
r <- 1
theta <- pi/2 * .9 # angle in radians
m <- matrix(nrow = 2, byrow = TRUE,
                   c(1, 0,
                     r*cos(theta), r*sin(theta)))
cosine_dist(m)
1 - cos(theta)

wai-wong-reimagine/mclm documentation built on May 16, 2019, 9:12 p.m.