How to make my code run faster?
Ältere Kommentare anzeigen
I'm new to matlab and this is one of the very first functions I wrote. My function works very well except that it's very slow (~27 seconds). How can I take advantage of Matlab's syntax to better optimize it?
function [ amin,bmin,cmin ] = perm( x,y,xmin )
A=(1:0.1:9);
B=(4:0.15:16);
C=(0.003:0.0005875:0.05);
result = 0.0;
amin = 15;
bmin = 16;
cmin = 0.05;
rmin = 1000;
temp = 0;
for i=1:81
for j=1:81
for k=1:81
result = -A(k)* erf((x-xmin)*C(i))+B(j);
Error = minus(y,result).^2;
temp = norm(Error);
if temp < rmin;
rmin = temp;
amin = A(k);
bmin = B(j);
cmin = C(i);
end
end
end
end
end
Akzeptierte Antwort
Weitere Antworten (1)
Jan
am 22 Jan. 2013
The general rule applies for all computer languages and the real life also:
Avoid repeated calculations!
rmin2 = rmin * rmin;
for i=1:81
c1 = erf((x-xmin)*C(i));
for j=1:81
for k=1:81
result = -A(k)* c1 +B(j);
Error = (y - result) .^ 2; % Is y a vector?
temp = sum(Error .* Error); % SQRT is more expensive then squaring
if temp < rmin2
rmin = temp;
rmin2 = rmin * rmin;
amin = A(k);
bmin = B(j);
cmin = C(i);
end
end
end
end
Kategorien
Mehr zu Object Analysis finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!