Substract Matrix coloum-wise from Matrix without using a loop

11 ビュー (過去 30 日間)
kaju
kaju 2019 年 11 月 7 日
コメント済み: kaju 2019 年 11 月 7 日
Hello,
is there a way to substract all coloumns of one matrix one by one from another matrix without using a loop?
I am trying to calculate the minimum distance of all the 3D points in matrix B to all the points in matrix A, where A = [109x3]; B = [109x3].
WITH a loop I would do it that way:
for n=109:-1:1
distance(n) = min(sqrt(sum((A - B(n,:)).^2,2)));
end
I was thinking of the repmat function but cant quite figure it out.
Thank your very much!

採用された回答

Stephen23
Stephen23 2019 年 11 月 7 日
編集済み: Stephen23 2019 年 11 月 7 日
permute lets you do this on one line:
D = min(sqrt(sum(bsxfun(@minus,permute(A,[1,3,2]),permute(B,[3,1,2])).^2,3)),[],1);
And testing against your code (using two matrices of random values):
>> isequal(distance,D)
ans = 1

その他の回答 (1 件)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 11 月 7 日
if you have Statistics and Machine Learning Toolbox :
distance=min(pdist2(A,B));
  1 件のコメント
kaju
kaju 2019 年 11 月 7 日
Sadly not, but thanks anyway!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by