I have a function that takes several scalar variables and outputs an array. The size of this array is always the same. I want to make a sweep of a few of the variables and store the output in a cell array.
According to documentation, arrayfun on GPUs support scalar expansion:
"A = arrayfun(FUN, B, C, ...) evaluates FUN using elements of arrays B, C, ... as input arguments with singleton expansion enabled. The inputs B, C, ... must all have the same size or be scalar. Any scalar inputs are scalar expanded before being input to the function FUN." http://www.mathworks.com/help/distcomp/arrayfun.html
But unfortunately this is not the case for MATLAB arrayfun. Is there an easy way around this?
Here is some code to illustrate what I'm trying to do:
% Declare some constants const1 = 1; const2 = 2; const3 = 3; const4 = 4;
% Declare variables to sweep sweep1 = 0:0.001:1; sweep2 = 0:0.01:1;
[mat1, mat2] = meshgrid(sweep1, sweep2);
% What I want to do, but can't % output is a 2D cell array [output] = arrayfun(@function, mat1, mat2, const1, const2, const3, const4, 'UniformOutput', false);
% What I end up having to do expander = ones(size(mat1)); const1 = const1(expander); const2 = const2(expander); const3 = const3(expander); const4 = const4(expander); [output] = arrayfun(@function, mat1, mat2, const1, const2, const3, const4, 'UniformOutput', false);
What I have right now works, but when sweep1 and sweep2 become very large, the memory needed for manual requirement becomes impractical. Is there a way to avoid manually expanding the scalars into massive matrices?
Any help would be appreciated.
I might not fully understand your question, but why don't you do something like:
output = arrayfun( ... @(m1,m2) myFunction(m1, m2, const1, const2, const3, const4), ... mat1, mat2, 'UniformOutput', false ) ;
"[...]this needs to be as fast as possible. " With R20112a loops are sometimes fast enough. Try this
%% N = 1e5; tic sqr_for = nan(N,1); for ii = 1 : N sqr_for(ii) = sqrt(ii); end toc %% tic sqr_arr = arrayfun( @sqrt , [1:N], 'uni', true ); toc %% max( abs( sqr_for - sqr_arr' ) )
Under some circumstances FOR loops are faster than vectorized code. arrayfun is not a vectorizing, because it still creates separated calls to the function. This happens, when large temporary array are created. Accessing the small RAM wastes much more time than operating on two vectors, which match into the processor cache.
Expanding the values explicitly by const4 = const4(expander) wastes time, because redundant data are created. Matlab's JIT seem to handle such expanding efficiently as long, as the data are used internally and to provided to user defined functions. Some tests seems to show, that here the expanding is not performed:
function dullTest x = rand(1, 1000); y = rand(1000, 1); tic; for k = 1:1000 S = x(ones(1, 1000), :) + y(:, ones(1, 1000)); end toc;
tic; for k = 1:1000 S = add(x(ones(1, 1000), :), y(:, ones(1, 1000))); end toc;
function s=add(x,y) s = x + y;
Of course the overhead for calling a subfunction matters also. But my impression of the avoided expansion concerns the memory foot print also, when huge arrays are used, which actually should fill my RAM completely.
While ARRAYFUN can be slower than a corresponding FOR loop, BSXFUN helps to avoid the creation of temporarily expanded data:
T = bsxfun(@plus, x, y);