How can I adapt arrayfun() to operate on vectors rather than scalars?
4 views (last 30 days)
Show older comments
I have a 45-variable function for which I'm trying to speed up computation of the finite-differencing based gradient, as function evaluation is expensive. I'm currently doing this with parfor on my 4-core CPU, but I'd still like things to be faster by using arrayfun() on my GPU (1050 Ti).
So I was thinking of creating a 45x45 matrix P where the ith column has the ith variable incremented by a small step size for finite differencing, and then calling
z = arrayfun(@objectiveFunction, P), and having arrayfun apply objectiveFunction to each column of P at a time.
However, arrayfun doesn't seem configured to do this, and I couldn't find another answer on the forums addressing whether its possible to reconfigure somehow.
Any thoughts?
Thanks.
2 Comments
Rik
on 2 Aug 2018
Usually it is easier to reshape the data to fit the needs of the function. You could also convert to a cell (with mat2cell) and use cellfun, or convert to a struct and use structfun (probably slower).
Accepted Answer
Joss Knight
on 2 Aug 2018
Look at pagefun for mixing in calls to vector operations with your ordinary element-wise functions. Usually with the functions supported by pagefun, standard MATLAB functions on gpuArray data, and arrayfun, you can get the result you're after.
2 Comments
Joss Knight
on 2 Aug 2018
Yeah, vecnorm isn't supported for gpuArray yet, but it's really simple piece of code:
norm_X = sqrt(sum(X.*X));
More Answers (0)
See Also
Categories
Find more on Linear Least Squares 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!