|
1 | 1 | function d = spPDist(spA,spB,p) |
2 | 2 | %PDIST p-norm distance between two sparse array structures. |
3 | 3 | % d = spPDist(spA,spB,p): The p-norm distance between two vectorized full |
4 | | -% arrays, represented as sparse array structures, with same numbers of |
5 | | -% entries. When there are only two arguments, p = 2 is the default, which |
6 | | -% gives the Euclidean distance between the two vectors; when p = 1, this |
7 | | -% function gives the taxicab distance; when p = inf, it gives the maximum |
8 | | -% difference. |
| 4 | +% arrays, each represented as a sparse array structure or as a full array. |
| 5 | +% They must have the same numbers of entries (including zeros). When there |
| 6 | +% are only two arguments, p = 2 is the default, which gives the Euclidean |
| 7 | +% distance between the two vectors; when p = 1, this function gives the |
| 8 | +% taxicab distance; when p = inf, it gives the maximum difference. |
9 | 9 | % |
10 | 10 | % Version 1.0 by Andrew J. Milne, The MARCS Institute, Western Sydney |
11 | 11 | % University, 2018-01-09 |
12 | 12 | % |
13 | 13 | % See also SPCOSSIM. |
14 | 14 |
|
15 | | -if ~isstruct(spA) || ~isstruct(spB) |
16 | | - error('Arguments must be (a cell array of) sparse array structures.') |
| 15 | +if nargin < 3 |
| 16 | + p = 2; |
| 17 | +end |
| 18 | +if nargin < 2 |
| 19 | + error('There must be exactly two arguments (arrays or sparse array structures).') |
| 20 | +end |
| 21 | + |
| 22 | +% Convert full array arguments to sparse array structures |
| 23 | +if ~isstruct(spA) |
| 24 | + spA = array2spArray(spA); |
| 25 | +end |
| 26 | +if ~isstruct(spB) |
| 27 | + spB = array2spArray(spB); |
17 | 28 | end |
18 | 29 |
|
19 | | -spDiff = spSum(spA,spScale(-1,spB)); |
| 30 | +spDiff = spPlus(spA,spTimes(-1,spB)); |
20 | 31 | diff = spDiff.Val; |
21 | 32 |
|
22 | 33 | % Calculate their p-norm distance |
|
0 commit comments