vectorisation of a loop

2 ビュー (過去 30 日間)
Zaid B
Zaid B 2021 年 11 月 30 日
編集済み: Matt J 2021 年 12 月 6 日
the matrix is "Sea" of m*n dimension .. this is the code i would like to vectorize :
=======================
S = size(Sea);
i = 1;
while i < S(1)
indices = find ( pdist2( Sea( i , : ),Sea( i+1:S(1) , :)) <0.05 );
Sea(indices , : ) = [];
S = size(Sea);
i = i+1;
end
===========
desc : i'm trying to calculate the distance between each row and all the other rows in the matrix and delete the ones that are closer than 0.05

回答 (2 件)

Matt J
Matt J 2021 年 11 月 30 日
編集済み: Matt J 2021 年 12 月 1 日
[m,n]=size(Sea);
D=pdist2(Sea,Sea);
D(1:m+1:end)=inf;
indices=any(triu(D<0.05),1);
Sea(indices,:)=[];
  12 件のコメント
Zaid B
Zaid B 2021 年 12 月 2 日
i got it why is nedded there but i think "i + find(...)" not "(i-1)+find(..)" because it should keep the current i , thank you that was a great help there ^^

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


Matt J
Matt J 2021 年 12 月 2 日
編集済み: Matt J 2021 年 12 月 2 日
I find this version to be about 30% faster:
S = size(Sea,1);
i = 1;
t=0.05^2;
while i < S
indices=true(1,S);
indices(i+1:end) = sum( abs( Sea( i , : )-Sea( i+1:S , :) ).^2,2) >= t ;
Sea=Sea(indices , : );
S = size(Sea,1);
i = i+1;
end
  13 件のコメント
Zaid B
Zaid B 2021 年 12 月 6 日
yeah i do , i ll try it and post the results later

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

Community Treasure Hunt

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

Start Hunting!

Translated by