Arrayfun with multidimensional input

5 visualizzazioni (ultimi 30 giorni)
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 Commenti
James Tursa
James Tursa il 9 Feb 2017
If "a" and "b" are vectors, what would the desired output be? A 4D array?
Kishore Rajendran
Kishore Rajendran il 9 Feb 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.

Accedi per commentare.

Risposta accettata

Edric Ellis
Edric Ellis il 9 Feb 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 Commento
Kishore Rajendran
Kishore Rajendran il 9 Feb 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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by