필터 지우기
필터 지우기

vectorisation of a loop

조회 수: 6 (최근 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일
편집: Zaid B 2021년 12월 2일
thank you for answering , but why ( i - 1 ) is added on that line?!
("Moreover, I don't think there is any more optimization that can be done" ... i thought so , but wanted to check if anyone has a better idea that would help)
Zaid B
Zaid B 2021년 12월 2일
편집: 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
Matt J
Matt J 2021년 12월 6일
편집: Matt J 2021년 12월 6일
If you have the Parallel Computing Toolbox, then making Sea a gpuArray should add some speed.
Zaid B
Zaid B 2021년 12월 6일
yeah i do , i ll try it and post the results later

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by