On vectorizing a function that takes vectors and scalars as inputs

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.

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 5 de Mzo. de 2016
Editada: Andrei Bobrov el 5 de Mzo. de 2016
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 comentarios

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 el 6 de Mzo. de 2016
Editada: Andrei Bobrov el 6 de Mzo. de 2016
I mean that X and Y - vectors with size [ m x 1 ] for each. Then XY = [X, Y] and XY0 = [X0, Y0].

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Mzo. de 2016

Editada:

el 6 de Mzo. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by