Result = cell([nv, 1]); % Avoid creation of {nv x nv} cell matrix
iR = 0;
n = numel(ini);
v = ini(:).'; % Current index vector as row
for k = 1:nvProd
% Call the function with the index vector as first input:
iR = iR + 1;
Result{iR} = fcn(v);
% Increase index vector:
for iv = 1:n
if v(iv) < fin(iv)
v(iv) = v(iv) + 1;
break; % Stop "for k" loop
end
v(iv) = ini(iv); % Reset this index
end
end
Well, I admit that this is not nice. Most of all it is not trivial to expand it to more dimensions and the permutation needed to join the output cell is ugly.
Best Answer