matlabloopsvectorizationfminsearch

MATLAB: Speed up fminbnd using vectorization


I am trying to optimize this piece of code. I am using the function fminbnd on a vector, splitting the task on its single entries using a loop.

Would it be possible to speed it up vectorizing the process?

    for i = 1:A
        for ii= 1:B
            for iii = 1:C
                fun = @(x) (x * variable(i,ii,iii))^2 ;
                [arg_min(i,ii,iii), min_(i,ii,iii)] = fminbnd(fun,-2,2);
            end
        end
    end

Thanks for the attention.

Sincerely

Luca


Solution

  • What if I discretize the domain of x? To provide an example:

    x = permute(repmat([-2:0.01:2]',[1,A,B,C]),[2,3,4,1]);
    variable2 = repmat(variable,[1,1,1,length([-2:0.01:2]')]);
    fun = (x.*variable2).^2;
    min_2 = min(fun,[],4);
    

    To check the result:

    max(max(max(abs(min_ - min_2))))
    

    The result is about the same.