You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-7Lines changed: 11 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,7 +69,7 @@ r = euclidean(x, y)
69
69
70
70
#### Computing distances between corresponding columns
71
71
72
-
Suppose you have two ``m-by-n`` matrix ``X`` and ``Y``, then you can compute all distances between corresponding columns of X and Y in one batch, using the ``colwise`` function, as
72
+
Suppose you have two ``m-by-n`` matrix ``X`` and ``Y``, then you can compute all distances between corresponding columns of ``X`` and ``Y`` in one batch, using the ``colwise`` function, as
73
73
74
74
```julia
75
75
r =colwise(dist, X, Y)
@@ -81,31 +81,35 @@ Note that either of ``X`` and ``Y`` can be just a single vector -- then the ``co
81
81
82
82
#### Computing pairwise distances
83
83
84
-
Let ``X`` and ``Y`` respectively have ``m`` and ``n`` columns. Then the ``pairwise`` function computes distances between each pair of columns in ``X`` and ``Y``:
84
+
Let ``X`` and ``Y`` respectively have ``m`` and ``n`` columns. Then the ``pairwise`` function with the ``dims=2`` argument computes distances between each pair of columns in ``X`` and ``Y``:
85
85
86
86
```julia
87
-
R =pairwise(dist, X, Y)
87
+
R =pairwise(dist, X, Y, dims=2)
88
88
```
89
89
90
90
In the output, ``R`` is a matrix of size ``(m, n)``, such that ``R[i,j]`` is the distance between ``X[:,i]`` and ``Y[:,j]``. Computing distances for all pairs using ``pairwise`` function is often remarkably faster than evaluting for each pair individually.
91
91
92
92
If you just want to just compute distances between columns of a matrix ``X``, you can write
93
93
94
94
```julia
95
-
R =pairwise(dist, X)
95
+
R =pairwise(dist, X, dims=2)
96
96
```
97
97
98
98
This statement will result in an ``m-by-m`` matrix, where ``R[i,j]`` is the distance between ``X[:,i]`` and ``X[:,j]``.
99
99
``pairwise(dist, X)`` is typically more efficient than ``pairwise(dist, X, X)``, as the former will take advantage of the symmetry when ``dist`` is a semi-metric (including metric).
100
100
101
+
For performance reasons, it is recommended to use matrices with observations in columns (as shown above). Indeed,
102
+
the ``Array`` type in Julia is column-major, making it more efficient to access memory column by column. However,
103
+
matrices with observations stored in rows are also supported via the argument ``dims=1``.
104
+
101
105
#### Computing column-wise and pairwise distances inplace
102
106
103
-
If the vector/matrix to store the results are pre-allocated, you may use the storage (without creating a new array) using the following syntax:
107
+
If the vector/matrix to store the results are pre-allocated, you may use the storage (without creating a new array) using the following syntax (``i`` being either ``1`` or ``2``):
104
108
105
109
```julia
106
110
colwise!(r, dist, X, Y)
107
-
pairwise!(R, dist, X, Y)
108
-
pairwise!(R, dist, X)
111
+
pairwise!(R, dist, X, Y, dims=i)
112
+
pairwise!(R, dist, X, dims=i)
109
113
```
110
114
111
115
Please pay attention to the difference, the functions for inplace computation are ``colwise!`` and ``pairwise!`` (instead of ``colwise`` and ``pairwise``).
0 commit comments