Arrayfun with multidimensional input

6 ビュー (過去 30 日間)
Kishore Rajendran
Kishore Rajendran 2017 年 2 月 8 日
コメント済み: Kishore Rajendran 2017 年 2 月 9 日
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
Kishore Rajendran 2017 年 2 月 9 日
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
Kishore Rajendran 2017 年 2 月 9 日
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 Center および File ExchangeGPU Computing in MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by