If I understood correctly, it may be simpler to ndgrid your arrays and have a single arrayfun
V = @(i,j,k,l) i*j/k+l;
I = 1:10; J = 1:20; K = 2:5; L = 3:7;
[II, JJ, KK, LL] = ndgrid(I, J, K, L);
sumall = sum(arrayfun(V, II(:), JJ(:), KK(:), LL(:)))
The downside of this method is that if your arrays are big enough, you're going to need a lot of memory for the ndgrid output.
A completely generic version of the above which works for n input arrays stored in a cell array:
fun = @(i,j,k,l) i*j/k+l;
arrays = {1:10, 1:20, 2:5, 3:7};
griddedarrays = cell(size(arrays));
[griddedarrays{:}] = ndgrid(arrays{:});
griddedarrays = cellfun(@(m) reshape(m, [], 1), griddedarrays, 'UniformOutput', false);
sumall = sum(arrayfun(fun, griddedarrays{:}));
PS: It's a shame bsxfun is limited to two inputs as it would avoid the ndgrid entirely.
Best Answer