Skip to content

Instantly share code, notes, and snippets.

@chao1224
Last active June 2, 2017 21:52
Show Gist options
  • Save chao1224/dd7fca70a9aea4fed06c45f541285f76 to your computer and use it in GitHub Desktop.
Save chao1224/dd7fca70a9aea4fed06c45f541285f76 to your computer and use it in GitHub Desktop.
%% Matrix defined by n * m
% Want to find k points
n = 256;
m = 256;
k = 5;
matrix = zeros(n, m);
%% Sample five points
[n, m] = size(matrix);
found_points_number = 0;
found_points_matrix = zeros(k, 2);
T = 100;
round = 0;
while found_points_number < k
%% sample at most T times for ony trial
round = round + 1;
% If cannont finish within T rounds, then restart whole sampling
if round >= T
round = 0;
found_points_number = 0;
end
sample_x = randi(n);
sample_y = randi(m);
accept_sample_point = true;
for c = 1:found_points_number
x = found_points_matrix(c,1);
y = found_points_matrix(c,2);
distance = sqrt( (x-sample_x).^2 + sqrt(y-sample_y).^2 );
if distance < 50
accept_sample_point = false;
end
end
if ~ accept_sample_point
continue
end
% Insert a point if it is acceptable
found_points_number = found_points_number + 1;
found_points_matrix(found_points_number,1) = sample_x;
found_points_matrix(found_points_number,2) = sample_y;
end
%% Fill in five points
found_points_matrix
for c = 1:k
x = found_points_matrix(c, 1);
y = found_points_matrix(c, 2);
matrix(x, y) = 1;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment