https://ww2.mathworks.cn/matlabcentral/fileexchange/90651-average-curve-of-multiple-curves-by-linear-interpolation

https://ww2.mathworks.cn/matlabcentral/answers/290707-average-multiple-curves-matlab

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
x1 = sort(rand(1, 10));                                 % Create Data
y1 = rand(size(x1)); % Create Data
x2 = sort(rand(1, 15)); % Create Data
y2 = rand(size(x2)); % Create Data

xi = linspace(0, 1, 50); % Create Vector Of Common X-Values

y1i = interp1(x1(:), y1(:), xi(:), 'linear', 'extrap'); % Interploate Or Extrapolate To New ‘x’ Values
y2i = interp1(x2(:), y2(:), xi(:), 'linear', 'extrap'); % Interploate Or Extrapolate To New ‘x’ Values

y_mean = mean([y1i y2i], 2); % Mean Of Y Values

figure(1)
plot(x1, y1, '-b')
hold on
plot(x2, y2, '-g')
plot(xi, y_mean, '-r', 'LineWidth',2)
hold off
grid
legend('Data 1', 'Data 2', 'Data Mean', 'Location','N')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
% ---------------------------------------------------------------------------- %
% DESCRIPTION %
% ----------- %
% Returns an average curve of multiple curves by linear interpolation. The %
% returned average curve also has unique and sorted abscissae. %
% ---------------------------------------------------------------------------- %
% INPUT %
% ----- %
% abscissae: %
% If this argument is a scalar, it specifies the number of abscissae on which %
% the average curve is evaluated, automatically ranging from the global %
% minimum to the global maximum abscissa considering all curves. If this %
% argument is a vector, the average curve is evaluated in the abscissae %
% specified by it. %
% %
% curves: %
% A cell array of length n containing the data of each curve, where n is the %
% number of curves. The data of each curve is defined by a two-column matrix, %
% where the first column corresponds to the abscissae and the second column %
% corresponds to the ordinates. Each curve may have a different number of %
% points. %
% ---------------------------------------------------------------------------- %
% OUTPUT %
% ------ %
% curve: %
% The abscissae and ordinates of the average curve in a two-column matrix. %
% ---------------------------------------------------------------------------- %

function curve = avgcurve(abscissae, curves)

% determine abscissae
if isscalar(abscissae)
xmin = +Inf;
xmax = -Inf;
for i = 1 : length(curves)
imin = min(curves{i}(:,1));
imax = max(curves{i}(:,1));
if imin < xmin, xmin = imin; end
if imax > xmax, xmax = imax; end
end
x = linspace(xmin, xmax, abscissae)';
else
x = abscissae(:);
end

% compute ordinates
y = zeros(size(x));
k = zeros(size(x));
for i = 1 : length(curves)
[xi, ii] = unique(curves{i}(:,1), "first");
yi = curves{i}(ii,2);
interp = interp1(xi, yi, x, "linear");
k = k + ~isnan(interp);
interp(isnan(interp)) = 0;
y = y + interp;
end
y = y ./ k;

% done
curve = [x y];