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

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Edric Ellis
Edric Ellis il 31 Ott 2014
Use bsxfun
bsxfun(@(x,y) x + y, gpuArray(1:4), gpuArray(1:4).')
  1 Commento
volkan
volkan il 3 Nov 2014
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?

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