Hi there, I'm working in a proyect in which I have to compute the euclidian distance between points, an operate with it in a specific way.The function I created takes as inputs:
-two vectors, which contain the (X,Y) coordinates of a set of points
-two scalars, which are the x0,y0 coordinates of another point
The function makes some operations that can be written in one single command, which I've simplified to make it more understandable
function [suma] = calculaSuma(X,Y,x0,y0)
acumulador=sum(sqrt((X-x0).^2+(Y-y0).^2));
end
Now if a want to evaluate the function with several (x0,y0) pairs what I'm doing is:
x0=rand(1,100)
y0=rand(1,100)
for n=1:100
calculaSuma(X,Y,x0(n),y0(n))
end
Is there a faster way to implement this code? I've tried vectorizing it using repmat, but since I'm working with big arrays it takes more time, and it doesn't take advantaje of the fact that X,Y are the same for al x0(n),y(0). Since this seems a very parallel computation I've also thought about using pagefun from the parallel computing toolbox, but it doesn't accept custom functions,only built in ones.So what can I do? I've search some information about the topic and I think that CUDA programming could be interesting for this case, but I don't know much about it.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 3 月 5 日
編集済み: Andrei Bobrov 2016 年 3 月 5 日

0 投票

XY -> array (X,Y) coordinates with size [m X 2];
XY0 -> array (X0,Y0) with size [n X 2]
out = squeeze(bsxfun(@hypot,XY,permute(XY0,[3,2,1])));

2 件のコメント

Fernando Rodriguez
Fernando Rodriguez 2016 年 3 月 6 日
Thanks for your answer Andrei, but I don't understand the beginning, when you set the size [n X 2]. I'd guess that m and n are the number of (X,Y) and (x0,y0) pairs respectively.But what does X stand for?
Andrei Bobrov
Andrei Bobrov 2016 年 3 月 6 日
編集済み: Andrei Bobrov 2016 年 3 月 6 日
I mean that X and Y - vectors with size [ m x 1 ] for each. Then XY = [X, Y] and XY0 = [X0, Y0].

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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