On vectorizing a function that takes vectors and scalars as inputs
Show older comments
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.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!