Difference between the individual rows of B and all the rows of A.

1 vue (au cours des 30 derniers jours)
Riccardo Rossi
Riccardo Rossi le 30 Nov 2018
Modifié(e) : Guillaume le 30 Nov 2018
Hi everyone.
I have 2 arrays, A (378812x3 double) and B (400x3 double).
I have to calculate the difference between the individual rows of B and all the rows of A.
I tried to use the following formula but it does not run.
How can i do it? Thanks
N=400
for k=1:N
DIF=sqrt((A(:,1)-B(1:N,1)).^2+(TA(:,2)-B(1:N,2)).^2+(A(:,3)-B(1:N,3)).^2);
end

Réponse acceptée

Bruno Luong
Bruno Luong le 30 Nov 2018
DIF = zeros(size(A,1),size(B,1))
for k=1:size(B,1)
DIF(:,k)=sqrt((A(:,1)-B(k,1)).^2+(A(:,2)-B(k,2)).^2+(A(:,3)-B(k,3)).^2);
end
Or simply (if you have the right toolbox)
DIF = pdist2(A,B)
  1 commentaire
Guillaume
Guillaume le 30 Nov 2018
Modifié(e) : Guillaume le 30 Nov 2018
You don't even need the toolbox, it's trivial to implement:
DIF = sqrt(sum((permute(A, [1 3 2]) - permute(B, [3 1 2])).^2, 3))
note that permute is an expensive operation so the loop may be faster.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by