Hello Community,
i need ur help, to speed up a routine.
i have n points in space and i need the distances between all points.
Here is the primitve script: ( n is normally some in the range of 1e5...)
n = 8;
xc = rand(n,1);
yc = rand(n,1);
r = zeros(n);
for i = 1:n
for j=1:n
if i~=j
r(i,j) = sqrt((xc(i)-xc(j))^2 + (yc(i)-yc(j))^2);
end
end
end
I know that the matrix r is symmetic so i need only to compute half of the elements. (This speed up to 50%)
n = 8;
xc = rand(n,1);
yc = rand(n,1);
r = zeros(n);
for i = 1:n
for j=1:n
if and(i~=j,i<j)
r(i,j) = sqrt((xc(i)-xc(j))^2 + (yc(i)-yc(j))^2);
end
end
end
toc
r = (r+r');
But it is possible to vectorize the whole routine?
Maybe with permute and a adjoint matrix which could be vectorised A(:) = ....
Thank you in Advance!

댓글 수: 2

Matt J
Matt J 2021년 4월 21일
편집: Matt J 2021년 4월 21일
( n is normally some in the range of 1e5...)
That sounds like a non-starter. The result would consume 37 GB in single floats. Even if you had this much free RAM, I suspect computing the matrix is not the most efficient approach for your application.It just doesn't sound like a reasonable thing to have to do.
Marko
Marko 2021년 4월 21일
Hello Matt,
sorry there is a mistake n should be in the order of 1e3!
I am writing a panel method, and estimatet the max. number of n-panels for an multi element airfoil.
So the Ram should not be the limiting factor.

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

 채택된 답변

Matt J
Matt J 2021년 4월 21일
편집: Matt J 2021년 4월 21일

1 개 추천

This should be pretty well optimized already,
r=pdist([xc,yc]);

댓글 수: 4

Marko
Marko 2021년 4월 21일
Hell Matt,
i testet your suggestion. Because I need the r(i,j) as a matrix:
r=squareform(pdist([xc,yc]));
Marko
Marko 2021년 4월 21일
here the fastest way up to n=10000
n= 10000
xc=rand(n,1);
yc=rand(n,1);
r=(xc.*ones(1,n)-ones(n,1).*xc').^2 + (yc.*ones(1,n)-ones(n,1).*yc').^2;
Matt J
Matt J 2021년 4월 21일
Faster:
r=(xc-xc').^2 + (yc-yc').^2;
Marko
Marko 2021년 4월 21일
편집: Marko 2021년 4월 21일
perfect, thank you Matt!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

태그

질문:

2021년 4월 21일

편집:

2021년 4월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by