Speeding up a slow running optimisaton problem

I'm trying to workout how I can run an optimisation problem faster. I tried to vectorize it (converting my multidimentional arrays to vectors) rather than run for loops but I don't think that's possible when going into the optimisation solver.
It's a spectral unmixing algorithm where I've measured a mixed substance containing two components at two wavelengths (F) then used a non-negative regression and their known individual spectra (S) to approximate their amounts (A) through time (i).
The data measured is organised in a multidimentional arrays which are the dimentions of the square image (332x332, rows by columns, r, c) through time (i) at start (st) and stop (sp) times.
Despite putting display off - I still get a report every fit "optimisation completed because..." - can that be surpressed?
for r=1:332
for c=1:332
for i=st:sp
F=[double(imdye(r,c,i));double(imzero(r,c,i))];
A = optimvar('A',size(S,2),'LowerBound',0);
residual = S*A - F;
obj = sum(residual.^2);
nonneglsq = optimproblem('Objective',obj);
opts = optimoptions(nonneglsq,'Display','off');
[sol,fval,exitflag,output] = solve(nonneglsq);
dyeamount(r,c,i)=sol.A(1);
flatamount(r,c,i)=sol.A(2);
end
end
end
Any suggestions are very welcome
Thanks

2 comentarios

Mike Croucher
Mike Croucher el 4 de Dic. de 2021
Is it possible to include all of your code so we can run it?
Sargondjani
Sargondjani el 4 de Dic. de 2021
I think you mix up two things:
  1. Solve an equation analytically with symbolic toolbox
  2. Solve a numerical problem
Ad 1) You can evaluate the symbolic solution for different values of your parameters. You only need to solve the equation once, and then substitute the parameter values into that solution with a loop.

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 4 de Dic. de 2021
Editada: Matt J el 4 de Dic. de 2021
Don't use the problem-based framework when speed is critical and don't repeat unnecessary commands inside the loops.
N=size(S,2);
lb=zeros(N,1);
A=lb;
for r=1:332
for c=1:332
for i=st:sp
F=[double(imdye(r,c,i));double(imzero(r,c,i))];
[A,fval,exitflag,output] = lsqlin(S,F,[],[],[],[],lb,[],A,opts);
dyeamount(r,c,i)=A(1);
flatamount(r,c,i)=A(2);
end
end
end

Categorías

Más información sobre Mathematics and Optimization en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Preguntada:

J
J
el 4 de Dic. de 2021

Editada:

el 4 de Dic. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by