MATLAB Answers

0

Speed up fminbnd using vectorization

Asked by Luca Gagliardone on 12 Aug 2017
Latest activity Edited by Matt J
on 24 May 2018
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,0,2);
end
end
end
Thanks for the attention.
Sincerely
Luca

  0 Comments

Sign in to comment.

3 Answers

Matt J 님의 답변 12 Aug 2017
 채택된 답변

In your example, the solution is always x=0, so a trivial vectorized solution would be
arg_min=zeros(A,B,C);
min_ = arg_min;
More generally, no, vectorization will not help in a situation like this. You could consider parallelizing the loop using PARFOR.

  0 Comments

Sign in to comment.


Luca Gagliardone 님이 편집함. 16 Aug 2017

What if I create a vector x containing all the possible values for minimization:
x = permute(repmat([0:0.01:2]',[1,A,B,C]),[2,3,4,1]);
variable2 = repmat(variable,[1,1,1,length(x)]);
fun = (x .* variable2).^2;
min_ = min(fun,[],4);
That would do an approximation of the above? Thanks.
Luca

  0 Comments

Sign in to comment.


Nick Durkee 님의 답변 24 May 2018
Matt J 님이 편집함. 24 May 2018

I actually developed a solution to this problem for my research. It's available on the file exchange.

  0 Comments

Sign in to comment.