Skip to content

Commit 4064f04

Browse files
committed
Allow for full array arguments; update help
1 parent 8f5008d commit 4064f04

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

spCosSim.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function s = spCosSim(spA,spB)
22
%SPCOSSIM Cosine similarity of two sparse array structures
33
% s = spCosSim(spA,spB): Cosine similarity of two vectorized full arrays,
4-
% represented by sparse array structures, with the same numbers of entries.
5-
% The output is a scalar.
4+
% each represented as a sparse array structure or as a full array. They must
5+
% have the same numbers of entries (including zeros). The output is a scalar.
66
%
77
% Version 1.0 by Andrew J. Milne, The MARCS Institute, Western Sydney
88
% University, 2018-01-09
@@ -11,12 +11,16 @@
1111

1212
persistent innerAA innerBB spALast spBLast
1313

14-
if ~isstruct(spA) || ~isstruct(spB)
15-
error('Arguments must be (a cell array of) sparse array structures.')
14+
if nargin ~= 2
15+
error('There must be exactly two arguments (arrays or sparse array structures).')
1616
end
1717

18-
if nargin ~= 2
19-
error('There must be exactly two sparse array structures.')
18+
% Convert full array arguments to sparse array structures
19+
if ~isstruct(spA)
20+
spA = array2spArray(spA);
21+
end
22+
if ~isstruct(spB)
23+
spB = array2spArray(spB);
2024
end
2125

2226
spANew = ~isequal(spA,spALast);

spInner.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
spA = varargin;
1616
elseif iscell(varargin{1})
1717
spA = varargin{:};
18-
else
19-
error('Arguments must be (a cell array of) sparse array structures.')
2018
end
2119
nSpA = size(spA,2); % count the number of arrays
2220

2321
if nSpA ~= 2
24-
error('There must be exactly two sparse array structures.')
22+
error('There must be two arguments (sparse array structures or full arrays).')
2523
end
2624

2725
% Convert full array arguments to sparse array structures

spPDist.m

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
function d = spPDist(spA,spB,p)
22
%PDIST p-norm distance between two sparse array structures.
33
% 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.
99
%
1010
% Version 1.0 by Andrew J. Milne, The MARCS Institute, Western Sydney
1111
% University, 2018-01-09
1212
%
1313
% See also SPCOSSIM.
1414

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);
1728
end
1829

19-
spDiff = spSum(spA,spScale(-1,spB));
30+
spDiff = spPlus(spA,spTimes(-1,spB));
2031
diff = spDiff.Val;
2132

2233
% Calculate their p-norm distance

0 commit comments

Comments
 (0)