|
1 | | -function [data, clustPoints, idx, centers, slopes, lengths] = ... |
| 1 | +function [data, clustPoints, idx, centers, angles, lengths] = ... |
2 | 2 | generateData( ... |
3 | | - slopeMean, ... |
4 | | - slopeStd, ... |
| 3 | + angleMean, ... |
| 4 | + angleStd, ... |
5 | 5 | numClusts, ... |
6 | 6 | xClustAvgSep, ... |
7 | 7 | yClustAvgSep, ... |
|
13 | 13 | ) |
14 | 14 | % GENERATEDATA Generates 2D data for clustering. Data is created along |
15 | 15 | % straight lines, which can be more or less parallel |
16 | | -% depending on the slopeStd parameter. |
| 16 | +% depending on the angleStd parameter. |
17 | 17 | % |
18 | | -% [data clustPoints idx centers slopes lengths] = |
19 | | -% GENERATEDATA(slopeMean, slopeStd, numClusts, xClustAvgSep, ... |
| 18 | +% [data clustPoints idx centers angles lengths] = |
| 19 | +% GENERATEDATA(angleMean, angleStd, numClusts, xClustAvgSep, ... |
20 | 20 | % yClustAvgSep, lengthMean, lengthStd, lateralStd, ... |
21 | 21 | % totalPoints, ...) |
22 | 22 | % |
23 | 23 | % Required input parameters: |
24 | | -% slopeMean - Mean slope of the lines on which clusters are based. |
25 | | -% Line slopes are drawn from the normal distribution. |
26 | | -% slopeStd - Standard deviation of line slopes. |
| 24 | +% angleMean - Mean angle in radians of the lines on which clusters are |
| 25 | +% based. Angles are drawn from the normal distribution. |
| 26 | +% angleStd - Standard deviation of line angles. |
27 | 27 | % numClusts - Number of clusters (and therefore of lines) to generate. |
28 | 28 | % xClustAvgSep - Average separation of line centers along the X axis. |
29 | 29 | % yClustAvgSep - Average separation of line centers along the Y axis. |
|
64 | 64 | % of each point. |
65 | 65 | % centers - Matrix (numClusts x 2) containing centers from where |
66 | 66 | % clusters were generated. |
67 | | -% slopes - Vector (numClusts x 1) containing the effective slopes |
| 67 | +% angles - Vector (numClusts x 1) containing the effective angles |
68 | 68 | % of the lines used to generate clusters. |
69 | 69 | % lengths - Vector (numClusts x 1) containing the effective lengths |
70 | 70 | % of the lines used to generate clusters. |
71 | 71 | % |
72 | 72 | % ---------------------------------------------------------- |
73 | 73 | % Usage example: |
74 | 74 | % |
75 | | -% [data cp idx] = GENERATEDATA(1, 0.5, 5, 15, 15, 5, 1, 2, 200); |
| 75 | +% [data cp idx] = GENERATEDATA(pi / 2, pi / 8, 5, 15, 15, 5, 1, 2, 200); |
76 | 76 | % |
77 | | -% This creates 5 clusters with a total of 200 points, with a mean slope |
78 | | -% of 1 (std=0.5), separated in average by 15 units in both x and y |
| 77 | +% This creates 5 clusters with a total of 200 points, with a mean angle |
| 78 | +% of pi/2 (std=pi/8), separated in average by 15 units in both x and y |
79 | 79 | % directions, with mean length of 5 units (std=1) and a "fatness" or |
80 | 80 | % spread of 2 units. |
81 | 81 | % |
82 | 82 | % The following command plots the generated clusters: |
83 | 83 | % |
84 | | -% scatter(data(:,1), data(:,2), 8, idx); |
| 84 | +% scatter(data(:, 1), data(:, 2), 8, idx); |
85 | 85 |
|
86 | 86 | % Copyright (c) 2012-2020 Nuno Fachada |
87 | 87 | % Distributed under the MIT License (See accompanying file LICENSE or copy |
|
93 | 93 |
|
94 | 94 | % Perform input validation |
95 | 95 | p = inputParser; |
96 | | -addRequired(p, 'slopeMean', ... |
| 96 | +addRequired(p, 'angleMean', ... |
97 | 97 | @(x) isnumeric(x) && isscalar(x)); |
98 | | -addRequired(p, 'slopeStd', ... |
| 98 | +addRequired(p, 'angleStd', ... |
99 | 99 | @(x) isnumeric(x) && isscalar(x) && (x >= 0)); |
100 | 100 | addRequired(p, 'numClusts', ... |
101 | 101 | @(x) isnumeric(x) && isscalar(x) && (x > 0) && (mod(x, 1) == 0)); |
|
118 | 118 | addParameter(p, 'pointOffset', ... |
119 | 119 | pointOffsets{2}, @(x) any(validatestring(x, pointOffsets))); |
120 | 120 |
|
121 | | -parse(p, slopeMean, slopeStd, numClusts, xClustAvgSep, yClustAvgSep, ... |
| 121 | +parse(p, angleMean, angleStd, numClusts, xClustAvgSep, yClustAvgSep, ... |
122 | 122 | lengthMean, lengthStd, lateralStd, totalPoints, varargin{:}); |
123 | 123 |
|
124 | 124 | % Check what pointDist was specified |
|
197 | 197 | yCenters = yClustAvgSep * numClusts * (rand(numClusts, 1) - 0.5); |
198 | 198 | centers = [xCenters yCenters]; |
199 | 199 |
|
200 | | -% Determine cluster slopes |
201 | | -slopes = slopeMean + slopeStd * randn(numClusts, 1); |
| 200 | +% Determine cluster angles |
| 201 | +angles = angleMean + angleStd * randn(numClusts, 1); |
202 | 202 |
|
203 | 203 | % Determine length of lines where clusters will be formed around |
204 | 204 | % Line lengths are drawn from the folded normal distribution |
|
213 | 213 | positions = distfun(lengths(i), clustPoints(i)); |
214 | 214 |
|
215 | 215 | % Determine (x, y) coordinates of point projections on the line |
216 | | - points_x = cos(atan(slopes(i))) * positions; |
217 | | - points_y = points_x * slopes(i); |
| 216 | + points_x = cos(angles(i)) * positions; |
| 217 | + points_y = sin(angles(i)) * positions; |
218 | 218 |
|
219 | 219 | if strcmp(p.Results.pointOffset, '1D') |
220 | 220 |
|
221 | 221 | % Get distances from points to their projections on the line |
222 | 222 | points_dist = lateralStd * randn(clustPoints(i), 1); |
223 | 223 |
|
224 | | - % Get perpendicular vectors to the current line for each point |
225 | | - perpVecs = [-sign(points_dist) * slopes(i) sign(points_dist)]; |
226 | | - % Normalize vectors |
227 | | - perpVecs = perpVecs / norm([slopes(i) 1]); |
| 224 | + % Get normalized vectors, perpendicular to the current line, for |
| 225 | + % each point |
| 226 | + perpAngles = angles(i) + sign(points_dist) * pi / 2; |
| 227 | + perpVecs = [cos(perpAngles) sin(perpAngles)]; |
| 228 | + |
228 | 229 | % Set vector magnitudes |
229 | 230 | perpVecs = abs(points_dist) .* perpVecs; |
230 | 231 |
|
|
0 commit comments