Is there a faster way to run my code?
Afficher commentaires plus anciens
Hello ,
i wrote the code bellow . Is there any faster and more efficient way to run this code (not using for-loop for example or something like that):
for count1=1:length(r)
for count2=1:length(C)
distance(count2,:)=abs(r(count1,:)-C(count2,:));
dist(count2)=sum(distance(count2,:),2);
end
[dist_hard index_hard(count1)]=min(dist);
end
The problem here is that when r or C contain many elements the code is slow and i its more than obvious that i dont want that .
Any help would be valuable .
Réponse acceptée
Plus de réponses (2)
Bruno Luong
le 23 Sep 2020
Modifié(e) : Bruno Luong
le 23 Sep 2020
[index_hard, dist_hard] = knnsearch(C,r,'K',1,'Distance','cityblock')
Bruno Luong
le 27 Sep 2020
Modifié(e) : Bruno Luong
le 27 Sep 2020
For binary arrays
r=rand(50,8)>0.5;
C=rand(60,8)>0.5;
[dmin, index_hard] = min(sum(xor(r,permute(C,[3 2 1])),2),[],3);
index_hard'
Catégories
En savoir plus sur Loops and Conditional Statements 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!