How to extract 6 neasrest neigbors for particles
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Abhinav Srivastava
le 11 Mai 2021
Commenté : Abhinav Srivastava
le 3 Juin 2021
Hello everyone.
I have a text file (kindly see the attached text file) containing neighbors of particles (ranging from 1 to 64). I want to extract 6 nearest neighbors of every particle in a seperate file.
Following is the sample from the output file. Column 1 and 2 show the particle number and column 3 show the distance.
1 19 0.874004
1 41 1.130905
1 42 1.131306
1 45 0.841402
1 50 1.393870
1 52 0.961277
1 61 0.802248
1 63 1.327221
1 64 1.408026
2 16 0.819803
2 33 1.153725
2 37 0.954796
2 39 1.439849
2 42 1.419793
2 58 1.140030
2 60 0.912613
2 63 1.454144
3 6 0.843783
3 7 1.071621
3 9 1.009899
3 10 1.254684
3 13 1.108856
3 14 1.015914
3 20 0.885181
3 35 1.263609
3 45 1.356776
3 51 1.268250
4 5 1.197906
4 40 1.238262
4 49 1.131004
4 53 1.207126
4 54 0.848770
4 57 1.278407
4 59 1.398415
4 62 0.782374
4 64 0.872153
The output which I want is
1 19 0.874004
1 41 1.130905
1 42 1.131306
1 45 0.841402
1 50 1.393870
1 52 0.961277
2 16 0.819803
2 33 1.153725
2 37 0.954796
2 39 1.439849
2 42 1.419793
2 58 1.140030
3 6 0.843783
3 7 1.071621
3 9 1.009899
3 10 1.254684
3 13 1.108856
3 14 1.015914
4 5 1.197906
4 40 1.238262
4 49 1.131004
4 53 1.207126
4 54 0.848770
4 57 1.278407
Any help will be appreciated.
Thanks in advance,
Abhinav
2 commentaires
Rik
le 11 Mai 2021
What have you tried already? This doesn't sound too hard with a loop. What issues did you have implementing it?
Réponse acceptée
DGM
le 11 Mai 2021
Modifié(e) : DGM
le 11 Mai 2021
This can probably be improved, but this was what I did:
A = [1 19 0.874004;
1 41 1.130905;
1 42 1.131306;
1 45 0.841402;
1 50 1.393870;
1 52 0.961277;
1 61 0.802248;
1 63 1.327221;
1 64 1.408026;
2 16 0.819803;
2 33 1.153725;
2 37 0.954796;
2 39 1.439849;
2 42 1.419793;
2 58 1.140030;
2 60 0.912613;
2 63 1.454144;
3 6 0.843783;
3 7 1.071621;
3 9 1.009899;
3 10 1.254684;
3 13 1.108856;
3 14 1.015914;
3 20 0.885181;
3 35 1.263609;
3 45 1.356776;
3 51 1.268250;
4 5 1.197906;
4 40 1.238262;
4 49 1.131004;
4 53 1.207126;
4 54 0.848770;
4 57 1.278407;
4 59 1.398415;
4 62 0.782374;
4 64 0.872153]
% sort by distance
[~,dmap] = sort(A(:,3),'ascend');
A = A(dmap,:);
npoints = numel(unique(A(:,1)));
idx = [];
for p = 1:npoints
% this will find at most 6 matches
idx = [idx find(A(:,1)==p,6)];
end
A = A(idx,:)
gives
A =
1 61 0.80225
1 45 0.8414
1 19 0.874
1 52 0.96128
1 41 1.1309
1 42 1.1313
2 16 0.8198
2 60 0.91261
2 37 0.9548
2 58 1.14
2 33 1.1537
2 42 1.4198
3 6 0.84378
3 20 0.88518
3 9 1.0099
3 14 1.0159
3 7 1.0716
3 13 1.1089
4 62 0.78237
4 54 0.84877
4 64 0.87215
4 49 1.131
4 5 1.1979
4 53 1.2071
12 commentaires
DGM
le 3 Juin 2021
Modifié(e) : DGM
le 3 Juin 2021
Sorry about that. The notifications on this site tend to show up out of order, so sometimes I completely miss followup questions if they don't show up until queue has moved several hours past their timestamp.
For example:
A = [1 19 0.874004;
1 41 1.130905;
1 50 1.393870;
1 63 1.327221;
1 64 1.408026;
2 16 0.819803;
2 33 1.153725;
2 37 0.954796;
2 39 1.439849;
2 42 1.419793;
2 58 1.140030;
2 60 0.912613;
2 63 1.454144;
3 6 0.843783;
3 7 1.071621;
3 13 1.108856;
3 35 1.263609;
3 45 1.356776;
4 5 1.197906;
4 40 1.238262;
4 49 1.131004;
4 53 1.207126;
4 54 0.848770;
4 57 1.278407;
4 59 1.398415;
4 62 0.782374;
4 64 0.872153]
n = 6; % minimum number of neighbors
[~,dmap] = sort(A(:,3),'ascend');
A = A(dmap,:);
npoints = numel(unique(A(:,1)));
idx = [];
for p = 1:npoints
% this will find at most n matches
% if there are fewer than n neighbors, skip it
thisblock = find(A(:,1)==p,n);
if numel(thisblock)>=n
idx = [idx thisblock];
end
end
A = A(idx,:)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Object Programming dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!