Arrayfun with multidimensional input

조회 수: 2 (최근 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 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