Arrayfun with multidimensional input

2 vues (au cours des 30 derniers jours)
Kishore Rajendran
Kishore Rajendran le 8 Fév 2017
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 commentaires
James Tursa
James Tursa le 9 Fév 2017
If "a" and "b" are vectors, what would the desired output be? A 4D array?
Kishore Rajendran
Kishore Rajendran le 9 Fév 2017
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.

Connectez-vous pour commenter.

Réponse acceptée

Edric Ellis
Edric Ellis le 9 Fév 2017
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 commentaire
Kishore Rajendran
Kishore Rajendran le 9 Fév 2017
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur GPU Computing in MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by