Skip to content

Commit f69fe12

Browse files
committed
Use angles instead of slopes
1 parent bcc9b08 commit f69fe12

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ depending on the selected input parameters.
1212
## Synopsis
1313

1414
```MATLAB
15-
[data, clustPoints, idx, centers, slopes, lengths] = ...
16-
generateData(slope, slopeStd, numClusts, xClustAvgSep, yClustAvgSep, ...
15+
[data, clustPoints, idx, centers, angles, lengths] = ...
16+
generateData(angleMean, angleStd, numClusts, xClustAvgSep, yClustAvgSep, ...
1717
lengthMean, lengthStd, lateralStd, totalPoints, ...)
1818
```
1919

@@ -23,8 +23,8 @@ depending on the selected input parameters.
2323

2424
Parameter | Description
2525
-------------- | -----------
26-
`slopeMean` | Mean slope of the lines on which clusters are based. Line slopes are drawn from the normal distribution.
27-
`slopeStd` | Standard deviation of line slopes.
26+
`angleMean` | Mean angle in radians of the lines on which clusters are based. Angles are drawn from the normal distribution.
27+
`angleStd` | Standard deviation of line angles.
2828
`numClusts` | Number of clusters (and therefore of lines) to generate.
2929
`xClustAvgSep` | Average separation of line centers along the X axis.
3030
`yClustAvgSep` | Average separation of line centers along the Y axis.
@@ -49,19 +49,19 @@ Parameter name | Parameter values | Default value | Description
4949
`clustPoints` | Vector (`numClusts` x *1*) containing number of points in each cluster.
5050
`idx` | Vector (`totalPoints` x *1*) containing the cluster indices of each point.
5151
`centers` | Matrix (`numClusts` x *2*) containing line centers from where clusters were generated.
52-
`slopes` | Vector (`numClusts` x *1*) containing the effective slopes of the lines used to generate clusters.
52+
`angles` | Vector (`numClusts` x *1*) containing the effective angles of the lines used to generate clusters.
5353
`lengths` | Vector (`numClusts` x *1*) containing the effective lengths of the lines used to generate clusters.
5454

5555
## Usage examples
5656

5757
### Basic usage
5858

5959
```MATLAB
60-
[data cp idx] = generateData(1, 0.5, 5, 15, 15, 5, 1, 2, 200);
60+
[data cp idx] = generateData(pi / 2, pi / 8, 5, 15, 15, 5, 1, 2, 200);
6161
```
6262

6363
The previous command creates 5 clusters with a total of 200 points, with
64-
a mean slope of 1 (*std*=0.5), separated in average by 15 units in both
64+
a mean angle of π/2 (*std*=π/8), separated in average by 15 units in both
6565
*x* and *y* directions, with mean length of 5 units (*std*=1) and a
6666
"fatness" or spread of 2 units.
6767

@@ -77,7 +77,7 @@ The following command generates 7 clusters with a total of 100 000 points.
7777
Optional parameters are used to override the defaults.
7878

7979
```MATLAB
80-
[data cp idx] = generateData(0, 0.1, 7, 25, 25, 25, 5, 1, 100000, ...
80+
[data cp idx] = generateData(0, pi / 16, 7, 25, 25, 25, 5, 1, 100000, ...
8181
'pointDist', 'norm', 'pointOffset', '1D', 'allowEmpty', true);
8282
```
8383

generateData.m

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
function [data, clustPoints, idx, centers, slopes, lengths] = ...
1+
function [data, clustPoints, idx, centers, angles, lengths] = ...
22
generateData( ...
3-
slopeMean, ...
4-
slopeStd, ...
3+
angleMean, ...
4+
angleStd, ...
55
numClusts, ...
66
xClustAvgSep, ...
77
yClustAvgSep, ...
@@ -13,17 +13,17 @@
1313
)
1414
% GENERATEDATA Generates 2D data for clustering. Data is created along
1515
% straight lines, which can be more or less parallel
16-
% depending on the slopeStd parameter.
16+
% depending on the angleStd parameter.
1717
%
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, ...
2020
% yClustAvgSep, lengthMean, lengthStd, lateralStd, ...
2121
% totalPoints, ...)
2222
%
2323
% 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.
2727
% numClusts - Number of clusters (and therefore of lines) to generate.
2828
% xClustAvgSep - Average separation of line centers along the X axis.
2929
% yClustAvgSep - Average separation of line centers along the Y axis.
@@ -64,24 +64,24 @@
6464
% of each point.
6565
% centers - Matrix (numClusts x 2) containing centers from where
6666
% clusters were generated.
67-
% slopes - Vector (numClusts x 1) containing the effective slopes
67+
% angles - Vector (numClusts x 1) containing the effective angles
6868
% of the lines used to generate clusters.
6969
% lengths - Vector (numClusts x 1) containing the effective lengths
7070
% of the lines used to generate clusters.
7171
%
7272
% ----------------------------------------------------------
7373
% Usage example:
7474
%
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);
7676
%
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
7979
% directions, with mean length of 5 units (std=1) and a "fatness" or
8080
% spread of 2 units.
8181
%
8282
% The following command plots the generated clusters:
8383
%
84-
% scatter(data(:,1), data(:,2), 8, idx);
84+
% scatter(data(:, 1), data(:, 2), 8, idx);
8585

8686
% Copyright (c) 2012-2020 Nuno Fachada
8787
% Distributed under the MIT License (See accompanying file LICENSE or copy
@@ -93,9 +93,9 @@
9393

9494
% Perform input validation
9595
p = inputParser;
96-
addRequired(p, 'slopeMean', ...
96+
addRequired(p, 'angleMean', ...
9797
@(x) isnumeric(x) && isscalar(x));
98-
addRequired(p, 'slopeStd', ...
98+
addRequired(p, 'angleStd', ...
9999
@(x) isnumeric(x) && isscalar(x) && (x >= 0));
100100
addRequired(p, 'numClusts', ...
101101
@(x) isnumeric(x) && isscalar(x) && (x > 0) && (mod(x, 1) == 0));
@@ -118,7 +118,7 @@
118118
addParameter(p, 'pointOffset', ...
119119
pointOffsets{2}, @(x) any(validatestring(x, pointOffsets)));
120120

121-
parse(p, slopeMean, slopeStd, numClusts, xClustAvgSep, yClustAvgSep, ...
121+
parse(p, angleMean, angleStd, numClusts, xClustAvgSep, yClustAvgSep, ...
122122
lengthMean, lengthStd, lateralStd, totalPoints, varargin{:});
123123

124124
% Check what pointDist was specified
@@ -197,8 +197,8 @@
197197
yCenters = yClustAvgSep * numClusts * (rand(numClusts, 1) - 0.5);
198198
centers = [xCenters yCenters];
199199

200-
% Determine cluster slopes
201-
slopes = slopeMean + slopeStd * randn(numClusts, 1);
200+
% Determine cluster angles
201+
angles = angleMean + angleStd * randn(numClusts, 1);
202202

203203
% Determine length of lines where clusters will be formed around
204204
% Line lengths are drawn from the folded normal distribution
@@ -213,18 +213,19 @@
213213
positions = distfun(lengths(i), clustPoints(i));
214214

215215
% 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;
218218

219219
if strcmp(p.Results.pointOffset, '1D')
220220

221221
% Get distances from points to their projections on the line
222222
points_dist = lateralStd * randn(clustPoints(i), 1);
223223

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+
228229
% Set vector magnitudes
229230
perpVecs = abs(points_dist) .* perpVecs;
230231

0 commit comments

Comments
 (0)