How to use arrayfun for nested loops without using memory multiple times for the same read only data

Greetings, I wish to use arrayfun for a function (call it FOO) that takes two different inputs in1 and in2. in1 comes from IN1 which is an m1 element vector and in2 comes from IN2 (m2 elements). I wish to compute FOO(IN1(ii),IN2(jj)) for all possible ii and jj pairs. One way would be:
[OUT1]=arrayfun(@FOO,repmat(IN1,m2,1),reshape(repmat(IN2',m1,1),m1*m2,1))
But this wastes the memory so needlessly. (we pass IN1 m2 times and IN2 m1 times and we can do not even edit them so they should be in the memory only once and should not waste more)
Do anyone of you have a good idea?
Best,
Volkan

 채택된 답변

Use bsxfun
bsxfun(@(x,y) x + y, gpuArray(1:4), gpuArray(1:4).')

댓글 수: 1

Greetings, Thanks for your reply and that answers the question I have posted, however, I now realize that I needed something that could take more than 2 inputs (15 inputs to be exact), if it exists something like
fun=@(x0,x1,x2,x3,...,x14)some non-associative function that cannot be reduced to two parameter case
bsxfun(fun, X0, X1, ..., X14)
I tried passing struct arrays and cells as inputs of bsxfun but it failed.
Any ideas about this one?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2014년 10월 31일

댓글:

2014년 11월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by