How to get random samples with certain distance between them?
Afficher commentaires plus anciens
Hello everybody,
I have the following problem: I have the following two colums which correspond to the x and y coordinate. It is like this but longer. (In my real case I have a matrix A(27889,2) which values goes from 0 to 5).
A = 0 0
1 0
2 0
3 0
4 0
0 1
1 1
2 1
3 1
4 1
0 2
1 2
My goal is to select random samples (2, 3, etc.) and that all samples are separated by a maximum and minimum distance (that is, between a range). I have made the following code that works perfectly, but it is not very robust due to the distance condition. This is because it is always selecting 3 random samples that are inside the range established, but if we make the range condition more and more reduced, the program is calculating for a long time.
n = prod(size(A));
nsemillas = 3; % For 3 random samples
min_dist = 2; % Minimum distance
max_dist = 5; % Maximum distance
while (true)
semillas_indexes = randperm(n,nsemillas);
[row,col] = ind2sub(size(A),semillas_indexes);
for i = 1:nsemillas
semilla(i,:) = A(row(1,i),:);
end
dist1 = sum(abs(semilla(1,:)));
dist2 = sum(abs(semilla(2,:)));
dist3 = sum(abs(semilla(3,:)));
dif1 = abs(dist1-dist2);
dif2 = abs(dist2-dist3);
dif3 = abs(dist1-dist3);
if dif1 > min_dist & dif2 > min_dist & dif3 > min_dist
if dif1 < max_dist & dif2 < max_dist & dif3 < max_dist
break;
end
else
continue;
end
end
I would like to know if there is any way to make the program more robust with this distance condition between samples.
Thanks in advance.
J.F.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Correlation and Convolution dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

