0001 function G = slnngraph(X, X2, nnparams, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 if nargin < 3
0067 raise_lackinput('slnngraph', 3);
0068 end
0069
0070 if ~isnumeric(X) || ndims(X) ~= 2
0071 error('sltoolbox:invalidarg', ...
0072 'X should be a 2D numeric matrix');
0073 end
0074
0075 if isempty(X2)
0076 X2 = [];
0077 else
0078 if ~isnumeric(X2) || ndims(X2) ~= 2
0079 error('sltoolbox:invalidarg', ...
0080 'A non-empty X2 should be a 2D numeric matrix');
0081 end
0082 end
0083
0084 if ~iscell(nnparams)
0085 error('stoolbox:invalidarg', ...
0086 'The parameters for slfindnn should be groupped in a cell array');
0087 end
0088
0089 opts.valtype = 'numeric';
0090 opts.sparse = true;
0091 opts.tfunctor = [];
0092 opts.sym = false;
0093 opts.symmethod = [];
0094 opts = slparseprops(opts, varargin{:});
0095
0096 switch opts.valtype
0097 case 'logical'
0098 islogic = true;
0099 case 'numeric';
0100 islogic = false;
0101 otherwise
0102 error('sltoolbox:invalidarg', ...
0103 'Invalid value type for graph: %s', opts.valtype);
0104 end
0105
0106
0107
0108
0109 n0 = size(X, 2);
0110 if isempty(X2)
0111 nq = n0;
0112 else
0113 nq = size(X2, 2);
0114 end
0115 can_sym = (n0 == nq);
0116
0117
0118 if ~islogic
0119 [nnidx, dists] = slfindnn(X, X2, nnparams{:});
0120 else
0121 nnidx = slfindnn(X, X2, nnparams{:});
0122 end
0123
0124
0125 edges = sladjlist2edgeset(nnidx, 0);
0126 edges = edges(:, [2,1]);
0127 clear nnidx;
0128 if ~islogic
0129 dists = vertcat(dists{:});
0130 else
0131 dists = [];
0132 end
0133
0134
0135 if ~isempty(dists)
0136 if isempty(opts.tfunctor)
0137 vals = dists;
0138 else
0139 vals = slevalfunctor(opts.tfunctor, dists);
0140 end
0141 else
0142 vals = [];
0143 end
0144 clear dists;
0145
0146
0147 G = slmakeadjmat(n0, nq, edges, vals, islogic, opts.sparse);
0148
0149
0150 if opts.sym && can_sym
0151 G = slsymgraph(G, opts.symmethod);
0152 end
0153
0154
0155
0156