How do i insert one value from one array to another array
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have 2 arrays with coordinates. For example
A = [1,2;2,1;1,1;1,3;3,2]
B = [0.5,0.5;2,2]
I compute for every point in A the nearest Point from B. I use dsearchn
k = dsearchn(B,A)
Now I want to give every point in B the next points from A. Making for every point in B a list of nearest points from A. Like point B(2,:) ans = 2 , 2 has the next points A(1,:),A(2,:),A(4,:) and A(5,:). I have no clue how to do it right and efficient.
2 commentaires
Réponse acceptée
Bruno Luong
le 2 Août 2022
See if s is what you want or not (not clear for me)
A = [1,2;2,1;1,1;1,3;3,2]
B = [0.5,0.5;2,2]
k = dsearchn(B,A)
AA = accumarray(k, (1:size(A,1))', [], @(r) {A(r,:)});
s = struct('B', num2cell(B,2), 'A', AA)
s(1).B
s(1).A
s(2).B
s(2).A
Plus de réponses (1)
David Hill
le 2 Août 2022
Look at pdist2
A = [1,2;2,1;1,1;1,3;3,2] ;
B = [0.5,0.5;2,2];
pdist2(A,B)
2 commentaires
David Hill
le 2 Août 2022
I don't understand your comment. Each column above gives the distance between each point in B with all the points in A. You could sort each column and then index into A to provide the sorted listing of the closest points of A to each point in B.
Voir également
Catégories
En savoir plus sur Matrices and Arrays 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!