How to improve interpolation performance

3 Ansichten (letzte 30 Tage)
Kevin Höhne
Kevin Höhne am 16 Okt. 2020
Bearbeitet: Rik am 16 Okt. 2020
Hi everyone,
I am using the shown function 'qinterplin1', which is much faster for my application cases. I'm only unhappy about some things:
  • Performance
  • Lots of 'if' queries
So here my questions:
  • Are there any alternatives for the commented lines, requiring most of the time within the script?
  • Is there a possibility to remove the differentation of "map_size"?
The input usually looks as foolowing:
x_vec = [25;80];
map = rand(2,4,4); % of course not random, but the numbers actually don't care
x_in = 25
or
x_vec = [1e-09,1e-08,2e-08,3e-08,4e-08,5e-08,6e-08,7e-08,8e-08,9e-08,1e-07,2e-07,3e-07,4e-07,5e-07,6e-07,7e-07,8e-07,9e-07,1e-06,0.001];
map = [0.0851,0.08510,0.07505,0.06510,0.05812,0.05111,0.04812,0.04512,0.04212,0.03913,0.03615,0.02038,0.04030,0.06520,0.08019,0.09129,0.1003,0.1103,0.12025,0.1302,0.1302];
x = 4.621e-9;
The script looks like this:
function [ret] = qinterplin1(x_vec, map, x)
map_size = ndims(map);
if map_size == 2
if any(size(map))==1 % 8.2% of time
map_size = 1;
end
end
use_dim = find(length(x_vec)==size(map)); % 10.4% of time
x(x<min(x_vec(:))) = min(x_vec(:)); % 17.1% of time
x(x>max(x_vec(:))) = max(x_vec(:)); % 13.9% of time
x0 = max(x_vec(x>=x_vec)); % 10.2% of time
x1 = min(x_vec(x<=x_vec));
if map_size==1
if any(x==x_vec) && x0==x1
ret = map(x_vec==x0);
return;
end
y0 = map(x_vec==x0);
y1 = map(x_vec==x1);
elseif map_size==2 && use_dim == 1
if any(x==x_vec) && x0==x1
ret = map(x_vec==x0);
return;
end
y0 = map(x_vec==x0,:);
y1 = map(x_vec==x1,:);
elseif map_size==3 && use_dim == 1
if any(x==x_vec) && x0==x1
ret = map(x_vec==x0,:,:);
return;
end
y0 = map(x_vec==x0,:,:);
y1 = map(x_vec==x1,:,:);
else
error('this case needs to be programmed');
end
ret = y0 + (x-x0) * (y1-y0) / (x1-x0);
end
Thanks in advance & kind regards
Kevin
  1 Kommentar
Rik
Rik am 16 Okt. 2020
Bearbeitet: Rik am 16 Okt. 2020
any(size(map))==1 %always returns true
any(size(map)==1) %doesn't always return true
You are calculating min(x_vec(:)) twice in one line, it might be worthwhile to calculate it once and store it in an intermediate variable.
temp=sort(x_vec(:));
x(x<temp(1)) = temp(1);
x(x>temp(end)) = temp(end);
For the rest I can't say much. Your function is too fast to reliably determine the time-consuming lines.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by