Arrayfun with multidimensional input

7 次查看(过去 30 天)
I have a function that take three inputs. The first two inputs (say 'a' and 'b') are integers, and the third input (M) is a 3D matrix. The function performs some computations on M using the integer inputs a and b (eg. dataOut = a + b .* (M)). Now let's say we have several values for a and b, stored as vectors while M is constant. I want to vectorize my function using arrayfun, but arrayfun requires the inputs (a,b,M) to be of the same size. I tried passing M as a structure that matches the size of a and b, but that doesn't work either. Eventually, if arrayfun works for this case, I would store a, b and M as gpuArrays and would like to run the code on GPU. Any help is appreciated.
  2 个评论
James Tursa
James Tursa 2017-2-9
If "a" and "b" are vectors, what would the desired output be? A 4D array?
Kishore Rajendran
Dear James Tursa. Thanks for your reply. Yes, the desired output will be a 4D array. Suppose a and b are (1 x 100) vectors, and M is 200 x 200 x 200, then the desired output will be 200 x 200 x 200 x 100. I can also do away with cell array as output which will be a (1 x 100) cell, and each cell element will have a 200 x 200 x 200 matrix.

请先登录,再进行评论。

采纳的回答

Edric Ellis
Edric Ellis 2017-2-9
You can do this by "binding" in M to your function handle, using an anonymous function, like so:
a = 1:10;
b = rand(1, 10);
M = rand(4);
arrayfun(@(aa, bb) norm(aa + bb.*M), a, b)
This sort of approach can work for gpuArray too, but beware that the functions that you are allowed to call inside gpuArray arrayfun is restricted to this list.
  1 个评论
Kishore Rajendran
Thanks Edric. Much appreciated. My function is a little more complicated than the norm you had used in your example. I will give it a try using my custom function and see if that would work.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 GPU Computing in MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by